Q. Write a program to print all prime numbers from 1 to 300. (Hint:  Use nested loops, break and continue).

Here is a Java program to print all prime numbers between 1 and 300 using nested loops, along with ‘break‘ and ‘continue‘ statements:

				
					public class PrimeNumbers {
    public static void main(String[] args) {
        System.out.println("Prime numbers between 1 and 300 are:");

        for (int num = 2; num <= 300; num++) {
            boolean isPrime = true;  // Assume the number is prime

            for (int i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    isPrime = false;  // num is not a prime number
                    break;  // No need to check further, exit the loop
                }
            }

            if (isPrime) {
                System.out.print(num + " ");  // Print the prime number
            }
        }

        System.out.println();
    }
}

				
			

Explanation:

  1. Initialization:

    • The outer for loop iterates through numbers from 2 to 300.
    • isPrime is used as a flag to determine if the current number is prime.
  2. Checking Primality:

    • The inner for loop checks if num is divisible by any number from 2 to num / 2. If it finds any divisor, it sets isPrime to false and breaks out of the loop.
  3. Printing Prime Numbers:

    • If isPrime remains true after the inner loop, it means the number is prime, and it is printed.
  4. Output:

    • The program will print all prime numbers between 1 and 300.

Sample Output:

				
					Prime numbers between 1 and 300 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293

				
			
prime number

Here is a Java program that uses the Scanner class to allow the user to specify the range for finding prime numbers. The program will print all prime numbers between 1 and the user-specified upper limit (which can be set to 300):

				
					import java.util.Scanner;

public class PrimeNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the upper limit to find prime numbers: ");
        int upperLimit = scanner.nextInt();

        System.out.println("Prime numbers between 1 and " + upperLimit + " are:");

        for (int num = 2; num <= upperLimit; num++) {
            boolean isPrime = true;  // Assume the number is prime

            for (int i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    isPrime = false;  // num is not a prime number
                    break;  // No need to check further, exit the loop
                }
            }

            if (isPrime) {
                System.out.print(num + " ");  // Print the prime number
            }
        }

        System.out.println();
        scanner.close();
    }
}

				
			

Out Put

				
					Enter the upper limit to find prime numbers: 300
Prime numbers between 1 and 300 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293

				
			

Compile and run this Java program. The program will prompt the user to enter an upper limit, and then it will print all prime numbers between 1 and that upper limit.

Scroll to Top