64: Write a program in C++ to find the last prime number occur before the entered number.

 

// Write a program in C++ to find the last prime number occur before the entered number.
//     Sample Output:
//     Input a number to find the last prime number occurs before the number: 50
//     47 is the last prime number before 50
//     **************************************************************************************************

#include <iostream>
using namespace std;
int main()
{
    int num;
    cout << "Input the number to find the last Prime number occurs before that number: ";
    cin >> num;

    int a, b;
    for (int i = 1; i < num; i++)
    {
        a=0;
        for (int j = 1; j <= i; j++)
        {
            if (i % j == 0)
            {
                a++;
            }
        }
        if (a == 2)
        {
            b = i;
        }
    }

    cout << b << " is the last prime number before " << num << endl;

    return 0;
}


Comments