C++ : a program to check whether given number is prime or not


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int n;
bool flag=0;

cout<<"Enter a number to check if prime or not : ";
cin>>n;

for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag =1;
break;
}
}
if(flag==0)
cout<<"\nThe number "<<n<<" is a prime number.";
else
cout<<"\nThe number "<<n<<" is not a prime number.";

getch();
return(0);
}

No comments:

Post a Comment