Program to check whether the given number is prime or not in C++
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int num,div,ul;
int prime;
cout<<"\nEnter the number :: ";
cin>>num;
if(num==1 || num==2)
prime=1;
else
{
if(num%2==0)
prime=0;
else
{
div=3;
ul=sqrt(num)+1;
while(div<=ul)
{
if(num%div==0)
{
prime=0;
break;
}
div+=2;
}
}
}
if(prime)
cout<<"\nThe number "<<num<< " is a Prime Number.";
else
cout<<"\nThe number "<<num<< " is not a Prime Number.";
getch();
}
OUTPUT::
Enter the number :: 3
The number 3 is a Prime Number.