C++ Program to Display Prime Numbers Between Two Intervals Using Functions

Here we are discussing a program to find all prime numbers between two numbers (entered by the user) are displayed on the output screen.

What is a prime number?

 A prime number (or prime integer, often simply called a "prime" for short) is a positive integer that has no positive integer divisors other than 1 and itself. E.g. 2, 3, 5, 7, 11.

How to check for a prime number?

There are various methods to determine whether a number is prime or not. The best way for finding prime numbers is by the factorization method. 
Factorization is the best way to find prime numbers. The steps involved in using the factorization method are:
•    Step 1: First find the factors of the given number
•    Step 2: Check the number of factors of that number
•    Step 3: If the number of factors is more than two, it is not a prime number.

C++ Program to Display Prime Numbers Between Two Intervals Using Functions

Here the program takes two positive integers from the user and displays prime numbers between these two intervals. To check prime numbers a function check_prime () is created. This function checks whether a number is prime or not. Ask the user to enter two positive integers and read the numbers to the variables n1 and n2. If the user enters a large number first, then we have to swap the numbers. Without swapping, this program won't work.
For swapping:
if (n1 > n2) 
    n2 = n1 + n2;
    n1 = n2 - n1;
    n2 = n2 - n1;

ex: n1 =8 and n2 = 2
if (8 > 2)
n2 = 8+2 = 10
n1 = 10-8 = 2
n2 = 10-2 = 8
now n1 = 2 and n2 = 8.

 Define the function to check whether the number is prime or not. All integers between n1 and n2 are passed to this function. If a number passed to check_prime() is a prime number, this function returns true, if not the function returns false.
We then iterate a loop from i = n1 + 1 to i < n2. In each iteration, we check whether i is a prime number or not.
At the beginning set a Boolean variable is_prime to true. 0 and 1 are not prime numbers, so first we have to check if the input number is 0 or 1. If the number is either 0 or 1 then the value of is_prime is set to false.
if ( n= 0 // n=1 )
{ is_prime = false }

Else the value of is_prime remains unchanged. Ie; is_prime = true;
Then check whether the number is perfectly divisible by other numbers or not. This can be done by a for  loop. Initialize the value of i =2. The loop runs from i =2 to i <= n/2 and increase the value of i by 1 with each iteration.
for ( i=2; i <= n/2; ++i )
The loop terminates at i= n/2 because we cannot find any factor for n beyond the number n/2. 
If the number entered by the user is perfectly divisible by i, then is_prime is set to false and the number will not be a prime number.
if ( n% i =0 )
{ is_prime = false; }

But if the entered number is not perfectly divisible by i throughout the entire loop, then it means that the input is only divisible by 1 and that number itself. So, the given input is a prime number.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Declare two integer type variables n1, n2 and Boolean type variable flag ;

Step 4: Ask the user to enter two positive integers;

Step 5: Read the number into the variable n1 and n2;

Step 6: Swap the two numbers if n1 > n2 

Step 7: Iterate a loop from  i = n1 + 1 to i < n2.
            Check whether the entered number is 0 or 1. If true, print n is not a prime number
             else, execute the loop to check if n is prime.
              initialize i=2 and continue the loop until i<= n/2;
              check if (n % i= 0 ) 
               if yes set is_prime = false;
               print n as a prime number if is_prime is true

Step 8: set the flag to true;

Step 9: print the result on the screen

Step 10: Exit

C++ Source Code

                                          #include <iostream>
using namespace std;

int check_prime(int);

int main() {

  int n1, n2;
  bool flag;

  cout << "Enter two positive integers: ";
  cin >> n1 >> n2;

  // swapping n1 and n2 if n1 is greater than n2
  if (n1 > n2) {
    n2 = n1 + n2;
    n1 = n2 - n1;
    n2 = n2 - n1;
  }

  cout << "Prime numbers between " << n1 << " and " << n2 << " are:\n";

  for(int i = n1+1; i < n2; ++i) {
    // if i is a prime number, flag will be equal to 1
    flag = check_prime(i);

    if(flag)
      cout << i << ", ";
  }

  return 0;
}

// user-defined function to check prime number
int check_prime(int n) {
  bool is_prime = true;

  // 0 and 1 are not prime numbers
  if (n == 0 || n == 1) {
    is_prime = false;
  }
  
  for(int j = 2; j <= n/2; ++j) {
    if (n%j == 0) {
      is_prime = false;
      break;
    }
  }

  return is_prime;
}
                                      

OUTPUT

Enter two positive integers: 10
40
Prime numbers between 10 and 40 are:
11, 13, 17, 19, 23, 29, 31, 37,