62: Write a program in C++ to find prime number within a range

 

// Write a program in C++ to find prime number within a range. Go to the editor
//     Input number for starting range: 1
//     Input number for ending range: 100
//     The prime numbers between 1 and 100 are:
//     2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
//     The total number of prime numbers between 1 to 100 is: 25
//     **************************************************************************************************

#include <iostream>
using namespace std;
int main()
{
    int start, end;
    cout << "\nInput number for starting range: ";
    cin >> start;
    cout << "Input number for ending range: ";
    cin >> end;
    cout << "\n**********************************************************\n\n";

    cout << "The Prime numbers between " << start << " and " << end << " are: ";
    int a,b=0;
    for (int i = start; i <= end; i++)
    {
        a = 0;
        for (int j = 1; j <= i; j++)
        {
            if (i % j == 0)
            {
                a++;
            }
        }
        if (a == 2)
        {
            cout << i << ' ';
            b++;
        }
    }
    cout << "\nThe total number of Prime numbers between " << start << " and " << end << " are: " << b;
    cout << "\n\n**********************************************************\n\n";

    return 0;
}


Comments