C++ Program to Check whether a number is a Prime number or not.

A Prime no is a whole number that is greater than 1 and only divisible by 1 and itself. 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29 are the first few prime numbers.

Today I will show you how to write a program to check whether a number is a Prime number or not.Let’s see the code…

C++ Program to Check whether a number is a Prime number or not.

#include<iostream>
using namespace std;

int main()
{
  int n, i;
  bool isPrime = true;

  cout << "Enter a positive integer: ";
  cin >> n;

  for(i = 2; i <= n / 2; i++)
  {
      if(n % i == 0)
      {
          isPrime = false;
          break;
      }
  }
  if (isPrime)
      cout << "This is a prime number";
  else
      cout << "This is not a prime number";

  return 0;
}

Output:C++ Program to Check whether a number is a Prime number or not.

Leave a Reply

Your email address will not be published. Required fields are marked *