# C++ Program to Display Prime Numbers Between Two Intervals

In this post, we will learn how to find and display prime numbers between two intervals using C++ Programming language.

This program asks the user to enter the minimum and maximum number of the range, then this program will find and print all the prime numbers lying between this range using a for loop and If Statement.

So, without further ado, let’s begin this tutorial.

## C++ Program to Display Prime Numbers Between Two Intervals

```// C++ Program to Display Prime Numbers Between Two Intervals
#include <iostream>
using namespace std;

int main(){
int min, max, isPrime;

cout << "Enter the first number: ";
cin >> min;
cout << "Enter the last number: ";
cin >> max;

// Displaying prime numbers between min and max
cout << "Prime numbers between " << min  << " and " << max << " are: " << endl;
for (int i = min + 1; i < max; i++){
isPrime = 0;
for (int j = 2; j < i/2; j++){
if (i % j == 0){
isPrime = 1;
break;
}
}
if (isPrime == 0){
cout << i << endl;
}
}
return 0;
}
```

Output

``````Enter the first number: 12
Enter the last number: 48
Prime numbers between 12 and 48 are:
13
17
19
23
29
31
37
41
43
47
``````

## How Does This Program Work ?

```    int min, max, isPrime;
```

In this program, we have declared three integer data type variables named min, max and isPrime.

```    // Asking for input
cout << "Enter the first number: ";
cin >> min;
cout << "Enter the last number: ";
cin >> max;
```

The user is asked to enter the minimum and maximum number of the range.

```    for (int i = min + 1; i < max; i++){
isPrime = 0;
for (int j = 2; j < i/2; j++){
if (i % j == 0){
isPrime = 1;
break;
}
}
if (isPrime == 0){
cout << i << endl;
}
}
```

We have used for loop and If statement to find all the prime numbers lying in the range. If any iteration contains a number which has factors, then the value of isPrime becomes 1.

Now, for all the iterations for whose the value of isPrime remains 0 are the prime numbers.

## Conclusion

I hope after going through this post, you understand how to display prime numbers between two intervals using C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.