Find prime numbers between two numbers in Java

In the following example of Java program to find all prime numbers between two given numbers :
Program :
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 + " ");
            }
        }
    }
}
Output :
Enter the lower bound: 3
Enter the upper bound: 12
Prime numbers between 3 and 12 are:3 5 7 11
* In this program, we first ask the user to input the lower and upper bounds using the Scanner class. We then use a for loop to iterate over all the numbers between the lower and upper bounds.

* For each number, we check whether it is a prime number using a similar approach to the previous program. If the number is prime, we print it to the console using System.out.print().

* Finally, we use System.out.println() to print a message indicating that the program has finished executing.