C++ Program to Check Whether a Number is Prime or Not

#include <iostream>
using namespace std;
int main()
{
  int n, i, m=0, flag=0;
  cout << "Enter the Number to check Prime: ";
  cin >> n;
  m=n/2;
  for(i = 2; i <= m; i++)
  {
      if(n % i == 0)
      {
          cout<<"Number is not Prime."<<endl;
          flag=1;
          break;
      }
  }
  if (flag==0)
      cout << "Number is Prime."<<endl;
  return 0;
}  

OUTPUT:

Enter the Number to check Prime: 17
 Number is Prime.   

Leave a Comment