17 Oct
17Oct


This program checks whether the given number is prime or not.If the number is not prime , not only it outputs that number is not prime but also it tells that on which prime numbers it can be divided.

#include 
using namespace std;

int main()
{
cout << "   This program is used to check whether the given number is prime or not." << "\n\n";
cout << "A prime number is a natural number, greater than 1 and that cannot be formed by multiplying two smaller natural numbers. Its positive divisors are 1 and itself." << "\n\n\n";

long long  x = 1, y = 1, z = 1, num = 0, counter1 = 2, counter = 2;

cout << "Enter the positive integer : ";
cin >> num;
cout << "\n\n";

if (num >= 1)
{
if (num == 1)
{
cout << "1 is not a prime number." << "\n\n";
cout << "Because it doesn't have exactly two positive divisors.";
}
else
{
while (counter < num)
{
y = num%counter;
if (y == 0)
{
z = 0;
}
counter++;
}
counter = 2;
if (z == 0)
{
cout << "Given number is not a prime number.";
cout << endl << "Given number is divisible by ";

while (counter < num)
{
y = num%counter;
if (y == 0)
{
z = 1;
counter1 = 2;
while (counter1 < counter)
{
x = counter%counter1;
if (x == 0)
{
z = 0;
}
counter1++;
}
if (z != 0)
{
cout << " " << counter;
}
}
counter++;

}
cout << ".";
}
else if (z != 0)
cout << "Given number is a prime number.";
}
}
else
{
cout << "Invalid Input.";
}

cout << "\n\n\nTo exit this console application, ";
system("pause");
return 0;
}

Click on the below link to download the executable file of How to Check Whether a Number is Prime or Not.

Prime Number.zip

Comments
* The email will not be published on the website.
I BUILT MY SITE FOR FREE USING