C++ program to check whether a given integer is prime number or not.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
void main(){
int i, n;
clrscr();
cout<<"enter a number"<<endl;
cin>>n;
if(n>0){
if(n==1){
cout<<" 1 is universal prime number"<<endl;
getch();
exit(0);
}
for(i=2; i<=n-1; i++){
if(n%i==0)
break;
}
if(n==i){
cout<<" is a prime number"<<endl;
}
else{
cout<<" is not a prime number"<<endl;
}
}
else{
cout<<"Wrong Input !! Try Again"<<endl;
}
getch();
}

No comments:

Post a Comment