 # C++ Programming Code Examples

## C++ > Mathematics Code Examples

### Program to Display Prime Numbers Between Two Intervals

``` Program to Display Prime Numbers Between Two Intervals Example to print all prime numbers between two numbers (entered by the user) in C++ Programming. This problem is solved using nested for loop and if...else statement. In this program, the while loop is iterated (high - low - 1) times. In each iteration, whether low is a prime number or not is checked and the value of low is incremented by 1 until low is equal to high. Visit this page to learn more on how to check whether a number is prime or not. If the user enters larger number first, this program doesn't work as intended. You can solve this issue by swapping the numbers if the user enters larger number first. #include <iostream> using namespace std; int main() { int low, high, j, flag; cout << "Enter two numbers(intervals): "; cin >> low >> high; cout << "Prime numbers between " << low << " and " << high << " are: "; while (low < high) { flag = 0; for(j = 2; j <= low/2; ++j) { if(low % j == 0) { flag = 1; break; } } if (flag == 0) cout << low << " "; ++low; } return 0; } ```