Program to find whether the number entered by user is prime or not.

This program checks whether a number is prime or not by dividing the entered number by all the numbers starting from 2 to the( entered number -1) and if the remainder is found to be 0 for any number then it will be displayed that the entered number is not a prime number.

But if none of the numbers have remainder as 0 then it will be displayed that the entered number is a prime number.

 

We will be displaying program which is compatible with Dev C++.

You can download dev c++ from here

 

Program Code:

 

 

#include<iostream.h>
#include<conio.h>
int main()
{
int a,b,c;
cout<<"Enter a number to check whether it's prime or not."<<endl;
cin>>a;
for(b=2;b<a;b++)
{  
     if(a==2 || a==1)
     {
             cout<<a<<" is a prime number.";
             return(0);
     }             
     if(a%b==0)
     {
               cout<<a<<" is not a prime number.";
               getch();
               return(0);
     }
     if(b==a-1)
     {
               cout<<a<<" is a prime number.";
     }
     
}
getch();

}

 

Output:

Prime or Not

 

 

 

Leave a Reply

Your email address will not be published.