C++ Program to check a number is prime or not


#include<iostream> using namespace std; int main(){ int p=1; int x,i=2; cout<<"\n Enter a number for check prime or not"; cin>>x; while(i <= x/2){ if(x%i == 0){ p=0; break; } i++; } if(p==1){ cout<<"\n The number you entered is prime "<<x; }else{ cout<<"\n The number you entered is not a prime "<<x; } return 0; }

Output:-

Enter a number for check prime or not 7
The number you entered is prime 7