# Java Program to Display Prime Numbers Between Two Intervals

In this program, we will learn to code the Java Program to Display Prime Numbers Between Two Intervals. In the previous tutorials, we have learned about prime numbers and we have also learned How to check whether a number is Prime or not in Java Programming Language.

If you had understood How to code the Java Program to check Prime Number from the previous tutorial, then this Java Program to Display Prime Numbers Between Two Intervals will be easy to understand.

Let’s get straight into the code of the Java Program to Display Prime Numbers Between Two Intervals.

Contents

## Java Program to Display Prime Numbers Between Two Intervals

```import java.util.*;
public class Main{

public static boolean isPrime(int number){
if(number == 1) return false;
if (number == 2) {
return true;
}
else {
int count = 0;
//logic
for (int div = 2; div * div <= number ; div++) {
if (number % div == 0) {
count++;
}
}
if(count > 0){
return false;
}
}

return true;
}

public static void main(String[] args){

Scanner sc = new Scanner(System.in);
System.out.println("Enter low : ");
int low = sc.nextInt();

System.out.println("Enter high : ");
int high = sc.nextInt();

System.out.println("All Prime Numbers from "+low+" to "+high+" are :");
for(int num = low ; num <= high ; num++){
if(isPrime(num) == true){
System.out.println(num);
}
}
}

}```

Output

``````Enter low : 5
Enter high : 20
All Prime Numbers from 5 to 20 are :
5
7
11
13
17
19``````

## How Does This Program Work ?

```        Scanner sc = new Scanner(System.in);
System.out.println("Enter low : ");
int low = sc.nextInt();

System.out.println("Enter high : ");
int high = sc.nextInt();```

In this program, we have taken the input of the low and the high of the intervals to print the prime numbers between the intervals using the Scanner class in Java.

```        System.out.println("All Prime Numbers from "+low+" to "+high+" are :");
for(int num = low ; num <= high ; num++){
if(isPrime(num) == true){
System.out.println(num);
}
}```

Then we have created a for loop which starts from `low` till `high` and then using the function `isPrime` we check if the number between low and high are prime or not and then we display the result.

This is the Java Program to Display Prime Numbers Between Two Intervals.

## Conclusion

I hope after going through this post, you understand Java Program to Display Prime Numbers Between Two Intervals and learned to code a Java Program to Check Prime Number.
If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.