C++ Program to Find Prime Number Or Not andrey вторник, 12 августа 2014 г. No Comment

This c++ program checks if a number is a prime or not. A number which is greater than 1 and can only be divided by 1 and itself are prime numbers. These prime numbers include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 and so on. A FOR loop is implemented in this program. If the number entered by the user can be divided by another number that is within its range to give zero as the remainder then, this number is not a prime number.


#include 
#include
void main()
{
int num, a=0;
cout<<"enter a number: ";
cin>>num;
for(int i=2;i<num;i++)//loop begins at 2 since 1 can divide any number
{
if(num%i==0)
{
a++;
}
}
if(a==0)
{
cout<<num<<" is a prime number";
}
else
cout<<num<<"is not a prime";
}
This c++ program checks if a number is a prime or not. A number which is greater than 1 and can only be divided by 1 and itself are prime numbers. These prime numbers include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 and so on. A FOR loop is implemented in this program. If the number entered by the user can be divided by another number that is within its range to give zero as the remainder then, this number is not a prime number.


#include 
#include
void main()
{
int num, a=0;
cout<<"enter a number: ";
cin>>num;
for(int i=2;i<num;i++)//loop begins at 2 since 1 can divide any number
{
if(num%i==0)
{
a++;
}
}
if(a==0)
{
cout<<num<<" is a prime number";
}
else
cout<<num<<"is not a prime";
}
by Jillur Rahman

Jillur Rahman is a Web designers. He enjoys to make blogger templates. He always try to make modern and 3D looking Templates. You can by his templates from Themeforest.

Follow him @ Twitter | Facebook | Google Plus

No Comment