Print Prime Numbers Between Intervals In C++

Exercise:

Write a C++ program to print all prime numbers between an interval of two numbers.

Method 1: When a smaller number is entered first for an interval

Click Here to View the Solution:
#include <iostream>
using namespace std;
int main() 
{
    int lower, upper;
    bool Prime = true;
    cout << "Enter an interval of two numbers:\n";
    cin >> lower >> upper;
    cout << "\nPrime numbers between " << lower << " and " << upper << " are: " << endl;
    while (lower < upper)
    {
        Prime = true;
        if (lower == 0 || lower == 1)
        {
            Prime = false;
        }
        else
        {
            for (int i = 2; i <= lower / 2; ++i) 
            {
                if (lower % i == 0)
                {
                    Prime = false;
                    break;
                }
            }
        }
        if (Prime)
        cout << lower << " ";
        ++lower;
    }
    return 0;
}
Click Here to View the Output:
Enter an interval of two numbers:
2
56
Prime numbers between 2 and 56 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53
Click Here to View the Explanation:
  • The while loop is entered if the first number entered lower is smaller than the second number entered upper.
  • It is checked if lower is a prime number and then the value of lower is increased by 1 through every iteration until it is equal to upper.
  • Every time lower is passed as a prime number, it is displayed on the screen.

Method 2: When a larger number is entered first for an interval

Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
    int lower, upper, TempVar;
    bool Prime;
    cout << "Enter an interval of two numbers:\n";
    cin >> lower >> upper;
    //swapping numbers if lower is greater than upper
    if (lower > upper) 
    {
        TempVar = lower;
        lower = upper;
        upper = TempVar;
    }
    cout << "\nPrime numbers between " << lower << " and " << upper << " are:" << endl;
    while (lower < upper)
    {
        Prime = true;
        if (lower == 0 || lower == 1)      // 0 and 1 are not prime numbers
        {
            Prime = false;
        }
        else
        {
            for (int i = 2; i <= lower / 2; ++i) 
            {
                if (lower % i == 0)
                {
                    Prime = false;
                    break;
                }
            }
        }
        if (Prime)
        cout << lower << " ";
        ++lower;
    }
    return 0;
}
Click Here to View the Output:
Enter an interval of two numbers:
32
6
Prime numbers between 6 and 32 are:
7 11 13 17 19 23 29 31
Click Here to View the Explanation:
  • Method 1 is similar to method 2.
  • The entered numbers are checked if lower is greater than upper through an if statement.
  • If a bigger number is entered before the smaller number, i.e. lower < upper, the condition of if statement is satisfied, and the variables are swapped.
  • Otherwise, the code proceeds like Method 1.
%d bloggers like this: