C++ Program to check whether a number is Prime or not

Program:


#include<iostream>
using namespace std;
int main()
{
int n, a=2;
bool r=false;
cout<<"Enter a number=";
cin>>n;
for(int a=2; a<n; a++)
{
if(n%a==0)
{
r=true;
}
}
if(r)
{
cout<<n<<" is not a prime number.";
}
else
{
if(n!=0&&n!=1)
{
cout<<n<<" is a prime number.";
}
else
{
cout<<"It is not a prime Number.";
}
}
return 0;
}

Output:





Comments

Post a Comment

Popular posts from this blog

C++ Program to take random numbers from user and calculate their sum