Wednesday, 1 March 2017

C++ program to check the prime number

// program to check the prime number
#include <iostream.h>
int main ()
{
  int num;
 
  cout<<”\n Enter any number to check prime number: ”;
  cin>>num;
 
  int p=1;
 
  for(int i=2;i<=num/2; i++)
  {
   if(num%i==0)
    {
      p=0; 
      break;
     }
   else
    {
      continue;
     }
  }
  
if(p!=0)
   cout<<”\n”<<num<<” is a prime number”;
else
   cout<<”\n”<<num<<” is not a prime number”;
 
 return 0;

}

No comments:

Post a Comment