Tuesday, December 22, 2015

Program to detect weather a number is prime or not

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int n, i;
bool flag=true;
cout<<"Enter Prime No:";
cin>>n;
if (n==2);
else if (n%2==0)
flag=false;
else
{
for (i=3;i<=n/2;i=i+2)
if (n%i==0)
{
flag=false;
break;
}

}
if (flag)
cout<<n<<" is prime\n";
else
cout<<n<<" is not prime\n";
return 0;
}

No comments:

Post a Comment