*Prime Numbers Between Two Intervals *

*Prime Numbers Between Two Intervals*

Write a C++ Program to find Prime Numbers Between Two Intervals using functions. Here’s simple C++ Program to find Prime Numbers Between Two Intervals using functions in C++ Programming Language.

*Numbers in C++*

*Numbers in C++*

Normally, when we work with Numbers, we use primitive data types such as int, short, long, float and double, etc. The number data types, their possible values and number ranges have been explained while discussing C++ Data Types.

Here is source code of the C++ Program to find Prime Numbers Between Two Intervals using functions. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.

**SOURCE CODE : :**

**SOURCE CODE : :**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* C++ Program to find Prime Numbers Between Two Intervals */ #include <iostream> using namespace std; int checkPrimeNumber(int); int main() { int n1, n2; bool flag; cout << "Enter 1st Interval :: "; cin >> n1 ; cout << "\nEnter 2nd Interval :: "; cin >> n2 ; cout << "\nPrime numbers between [ " << n1 << " and " << n2 << " ] are :: \n\n"; for(int i = n1+1; i < n2; ++i) { // If i is a prime number, flag will be equal to 1 flag = checkPrimeNumber(i); if(flag == false) cout << i << " "; } return 0; } // user-defined function to check prime number int checkPrimeNumber(int n) { bool flag = true; for(int j = 2; j <= n/2; ++j) { if (n%j == 0) { flag = false; break; } } return flag; } |

**OUTPUT : :**

**OUTPUT : :**

1 2 3 4 5 6 7 8 9 10 11 12 13 |
/* C++ Program to find Prime Numbers Between Intervals */ Enter 1st Interval :: 10 Enter 2nd Interval :: 60 Prime numbers between [ 10 and 60 ] are :: 12 14 15 16 18 20 21 22 24 25 26 27 28 30 32 33 34 35 36 38 39 40 42 44 45 46 48 49 50 51 52 54 55 56 57 58 Process returned 0 |

Above is the source code for C++ Program to find Prime Numbers Between Intervals which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may * Contact Us* through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.

**Thanks for reading the post….**

This wrong. In main it should be flag = false inside for loop instead of flag = true.

I think this is wrong prime number means it should be divisible by 1 and itself. But in above program there are numbers which are not prime I request u to change the program and keep it in the website