 Home >>Java Programs >Find Prime numbers between two numbers in Java

# Find Prime numbers between two numbers in Java

### 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;
}
}

```
```
Output:
Enter the first number : 1
Enter the second number : 39
List of prime numbers between 1 and 39
2
3
5
7
11
13
17
19
23
29
31
37