# C++ Program to Display or Print Prime Numbers Between a Range or an Interval

In this program, we will learn how to display or print the Prime Numbers between a given range using the C++ programming language.

I have already discussed **how to check whether a number is prime or no**t in previous posts. So, check it out for better understanding.

I will be discussing different programs

- Between an Interval.
- In Range 1 to N

### C++ Programming Code to Display or Print Prime Numbers Between a Range or an Interval

In both the programs below, I have used the different approaches in the inner for loop.

In one, I have traversed until half of the number **(i/2)**. And in other, I have traversed till the square root of the number (**sqrt(i)**).

### Between an Interval

In this program, user is asked to give the start and end of the range or interval.

And then I apply the **for** loop in that range and check every number in between the range including the ends also whether they are a prime number or not.

### Code:-

#include<iostream> using namespace std; int main(){ int start, end; bool isPrime; cout << "Enter starting number of the interval : "; cin >> start; cout << "Enter ending number of the interval : "; cin >> end; cout << "Prime Numbers Between " << start << " and " << end << " : " << endl; for(int i=start; i<=end; i++){ isPrime = true; if(i <= 1){ isPrime = false; }else{ for(int j = 2; j <= i/2; j++){ if(i % j == 0){ isPrime = false; break; } } } if (isPrime) cout << i << " "; } cout << endl; return 0; }

### Output:-

Enter ending number of the interval : 55

Prime Numbers Between 5 and 55 :

5 7 11 13 17 19 23 29 31 37 41 43 47 53

### In Range 1 to N

In this program, I have asked for only the end of the range.

And then with the help of **for** loop, numbers are checked.

### Code:-

#include<iostream> #include<math.h> using namespace std; int main(){ int end; bool isPrime; cout << "Enter the end of the range : "; cin >> end; cout << "Prime Numbers Between 1" << " and " << end << " : " << endl; for(int i=2; i<=end; i++){ isPrime = true; for(int j = 2; j <= sqrt(i); j++){ if(i % j == 0){ isPrime = false; break; } } if (isPrime) cout << i << " "; } cout << endl; return 0; }

### Output:-

Prime Numbers Between 1 and 55 :

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53

### You can learn about many other C++ Programs Here.

#### Best Books for learning C++ programming language with Data Structure and Algorithms.

#### Amit Rawat

#### Latest posts by Amit Rawat (see all)

- Python Program to Print the Fibonacci Sequence (2 ways) - April 7, 2020
- Python Program to Display or Print Prime Numbers Between a Range or an Interval - June 18, 2019
- Python Program To Print Pascal’s Triangle (2 Ways) - June 17, 2019