C++ Program to Check Prime Number

  • Write a C++ program to check whether a number is prime number or not.
  • How to test whether a number is prime number(Primality testing).

C++ Program to check Prime number


using namespace std;

int main() {
  int num, i, isPrime=0;
  cout << "Enter a Positive Integer\n";
  cin >> num;
  // Check whether num is divisible by any number between 2 to (num/2)
  for(i = 2; i <=(num/2); ++i) {
      if(num%i==0) {
      cout << num << " is a Prime Number";
      cout << num << " is not a Prime Number";
  return 0;
Enter a Positive Integer
23 is a Prime Number
Enter a Positive Integer
21 is not a Prime Number

Recommended Posts
C++ Program to Check for Armstrong Number
C++ Program to Display Armstrong Number Between Two Intervals
C++ Program to Check Prime Number Using Function
C++ Program to Check whether a string is Palindrome or Not
C++ Program to Find Power of Number using Recursion
C++ Program to Find GCD or HCF of Two Numbers Using Recursion
C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers
C++ Program to Find Factorial of a Number
C++ Program to Check Leap Year
C++ Program to Make a Simple Calculator
C++ Program to Display Fibonacci Series using Loop and Recursion
All C++ Programs