**Exercise**:

Write a Java Program to display prime numbers between two intervals.

**Click Here to View the Solution!**

`public`` `

class FindPrimeNumbers `{`

` `

` `

public static void main(String[] args) `{`

` `

` `

int` low = `

40`, high = `

98`;`

` `

` `

while` (low < high) {`

` `

boolean` flag = `

false`;`

` `

` `

for`(`

int` i = `

2`; i <= low/`

2`; ++i) {`

` `

// condition to check for non prime number
` `

if`(low % i == `

0`) {`

` flag = `

true`;`

` `

break`;`

` }`

` }`

` `

if` (!flag && low != `

0` && low != `

1`)`

` System.out.print( low + `

" "`);`

` ++low;`

` }`

` }`

`}`

**Click Here to View the Output!**

41 43 47 53 59 61 67 71 73 79 83 89 97

**Click Here to View the Explanation!**

- This program is used for displaying the prime numbers between the high and low intervals by using a while and a for loop.
- Initially, the two intervals are introduced in the program as integer variables
`low = 40`

and`high = 98`

. - A
`while loop`

includes an inner`for loop`

in its body. The`while loop`

holds the condition that until the low interval remains smaller than the high interval, the inner`for loop`

will execute. - This program relies on the
`for loop`

which tests each number in an interval between low and high to find out that whether a number is a prime number or not. - Every time a prime number is checked regarding an interval, the value of the
`flag = false`

needs to be reset in every iteration of a`while loop`

. - It is to be noted, after exiting the for loop, if a number is being in an interval starting from 0 till 10, the number 0 and 1 are to be excluded from the interval since 0 and 1 can never be prime numbers.

- The value of the low interval will be decremented after the conditional if statement ends. And the while loop will continue again until all the prime numbers are found and printed.