In this program, you'll learn to display all prime numbers between the given intervals using a function in Java.

To find all prime numbers between two integers, `checkPrimeNumber()`

function is created. This function checks whether a number is prime or not.

```
public class Prime {
public static void main(String[] args) {
int low = 20, high = 50;
while (low < high) {
if(checkPrimeNumber(low))
System.out.print(low + " ");
++low;
}
}
public static boolean checkPrimeNumber(int num) {
boolean flag = true;
for(int i = 2; i <= num/2; ++i) {
if(num % i == 0) {
flag = false;
break;
}
}
return flag;
}
}
```

When you run the program, the output will be:

23 29 31 37 41 43 47

In the above program, we've created a function named `checkPrimeNumber()`

which takes a parameter `num` and returns a boolean value.

If the number is prime, it returns `true`

. If not, it returns `false`

.

Based on the return value, number is printed on the screen inside `main()`

function.

It takes a lot of effort and cost to maintain Programiz. We would be grateful if you support us by either:

**Disabling AdBlock on Programiz. We do not use intrusive ads.**

or

Donate on Paypal
By using Programiz, you agree to use cookies as stated in our Privacy policy Continue