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


January 22, 2023, Learn eTutorial
1415

What is a prime number?

In C++, we can define a prime number (or prime integer, often simply called a "prime" for short) as a positive integer that has no divisors other than 1 and itself. E.g. 2, 3, 5, 7, 11.

how to Display Prime Numbers Between Two Intervals Using Functions using C++

Here the program takes two positive integers from the user and displays prime numbers between these two numbers. For that we have to check each number between this interval and find if it is prime or not. To check prime numbers, a function check_prime () is created. This function checks the given 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.

Now, we have to open a loop to get each number from ni to n2 and pass this number to the function we have created. This function returns true if it is a prime number, if it is a prime number we have to print that number and continue the loop.

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 variables 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,