Q:

C++ Program to find Prime Numbers Between Two Intervals

belongs to collection: C++ Number Solved Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Number Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Program to Convert Binary Number to Octal... >>
<< C++ Program to Convert Decimal Number to Binary...