import java.util.Scanner;
public class PrimeNumbersInRange {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the lower bound: ");
int lowerBound = input.nextInt();
System.out.print("Enter the upper bound: ");
int upperBound = input.nextInt();
System.out.println("Prime numbers between " + lowerBound + " and " + upperBound + " are:");
for (int i = lowerBound; i <= upperBound; i++) {
boolean isPrime = true;
if (i == 1) {
isPrime = false;
} else {
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
}
}
Enter the lower bound: 3
Enter the upper bound: 12
Prime numbers between 3 and 12 are:3 5 7 11
Scanner
class. We then use a for
loop to iterate over all the numbers between the lower and upper bounds.System.out.print()
.System.out.println()
to print a message indicating that the program has finished executing.