Find whether Given number is a Prime number or not (C++)





Here in this program we will find out whether the given number is a prime number or not. Lets have an initial idea about this. To find whether the given number is prime number or not, give a number for example enter 57. It is pretty easy to find this kind of solution for smaller numbers. But for larger numbers it’s bit difficult. So use the below code to check whether the Given number is a Prime number or not.
Program:

#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;

}

if you find any bug in the above program. let us know about it. Please post your complaint at pctech@gadgetcage.com.


We'll send more interesting posts like Find whether Given number is a Prime number or not (C++) to you!
Enter your Email Address:
Join us on Facebook.

Speak Your Mind

*