Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Is Prime Number

#include<iostream.h>
#include <math.h>

void main()
{
int number;
cout<<" ---------------------------------------------"<<endl;
cout<<" Enter a number to find if its a prime number "<<endl;
cout<<" ---------------------------------------------"<<endl;
cin>>number;
bool a =true;

for(int i=2;i<sqrt(number);i++) //check untill the square root
{

if(number%i==0) // if it is divisible it is non prime
{
a=false;
break;
}
}


if(a==false)
cout<<number<<" is not a prime number"<<endl;
else
cout<<number<<" is a prime number"<<endl;

}

No comments:

Post a Comment

Search 24 Bytes