Sunday, March 8, 2015

Prime number --- yes or no

Script

#include <iostream>
using namespace std;

main()
{
        int number, k, flag, remainder;
        cout << "Enter a number:  ";
        cin >> number;
        k = 2;
        flag = 1;

        while ((k <= number / 2) && (flag == 1))
        {
                remainder = number % k;
                if (remainder == 0)
                        flag = 0;
                k++;
        }

        if (flag == 1)
                cout << number << " is a prime number." << endl;
        else
                cout << number << " is not a prime number." << endl;

        return 0;
}

Execution

Enter a number:  37
37 is a prime number.


Enter a number:  41
41 is a prime number.


Enter a number:  36
36 is not a prime number.

No comments:

Post a Comment