C++ program to check whether a number is prime or not.Today we make a Simple Program how to find the prime number using c++.
What is a PRIME NUMBER?
” A Natural number greater than 1 which has only two divisor 1 and itself is called prime number “.
Example to check whether an integer (entered by the user) is a prime number or not using for loop and if…else statement.
C++ Program to Check Whether a Number is Prime or Not
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int x;
for(int i=1;i<=100;i++){
cout<<“Enter the number”<<endl;
cin>>x;
if(x==2 || x==3 || x==5){
cout<<“Number is prime”<<endl;
}
else if(x%2==0 || x%3==0 || x%5==0)
{
cout<<“Not prime”<<endl;
}
else
{
cout<<“Number is prime”<<endl;
}
}
getch();
return 0;
}
Output of Program
Enter the number 55 Not prime Enter the number 5 Number is prime Enter the number