Home >>Java Programs >Find Prime numbers between two numbers in Java
In this example, we will see a Java program through which we can find all the Prime numbers present between two given numbers.
Program
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number : ");
int start = s.nextInt();
System.out.print("Enter the second number : ");
int end = s.nextInt();
System.out.println("List of prime numbers between " + start + " and " + end);
for (int i = start; i <= end; i++)
{
if (isPrime(i))
{
System.out.println(i);
}
}
}
public static boolean isPrime(int n)
{
if (n <= 1)
{
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
}