C++ Program to find Prime Numbers Between Two Intervals

By | December 25, 2016

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++


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 : :


/* 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 : :


/* 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….

Leave a Reply