C++ Program to Check Whether a Number is Prime or Not
13/04/2024Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is a number that cannot be exactly divided by any number other than 1 and itself. The task of determining whether a given number is prime is fundamental in various fields of computer science, cryptography, and mathematical computation.
Understanding Prime Numbers
Before delving into the program, it’s essential to understand the characteristics of prime numbers and the logic used to identify them. A prime number:
 Must be greater than 1.
 Has exactly two distinct positive divisors: 1 and itself.
For example, the numbers 2, 3, 5, 7, 11, and 13 are prime because each can only be divided by 1 and the number itself without leaving a remainder. On the other hand, numbers like 4, 6, 8, 9, and 10 are not prime because they have more than two divisors.
Algorithm for Checking Prime Numbers
The simplest method to check if a number is prime is to try dividing it by all numbers from 2 to the number itself (exclusive) and check for a remainder. If any division operation results in no remainder, the number is not prime. However, this method can be optimized. Since a factor of a number n cannot be more than n/2 (except for n itself), you only need to check up to n/2. Further optimization notes that a factor of n cannot be more than √n, significantly reducing the number of checks needed.
C++ Program to Determine Prime Numbers
Let’s now look at a C++ program that implements the optimized approach to check if a given number is prime.
#include <iostream>
#include <cmath> // For the sqrt() function
using namespace std;
// Function to check if a number is prime
bool isPrime(int num) {
// Handle base cases: if num is less than 2, it’s not prime
if (num < 2) {
return false;
}
// Only need to check for divisors up to the square root of num
for (int i = 2; i <= sqrt(num); ++i) {
// If num is divisible by any number between 2 and sqrt(num), it’s not prime
if (num % i == 0) {
return false;
}
}
// If no divisors were found, num is prime
return true;
}
int main() {
int num;
cout << “Enter a number: “;
cin >> num;
// Check if the number is prime and output the result
if (isPrime(num)) {
cout << num << ” is a prime number.” << endl;
} else {
cout << num << ” is not a prime number.” << endl;
}
return 0;
}
Detailed Explanation

Function isPrime:
This function takes an integer num as input and returns true if num is prime; otherwise, it returns false. It first handles the base case—if num is less than 2, it returns false because prime numbers are greater than 1. Then, it checks for divisors of num from 2 up to the square root of num (inclusive). This range suffices due to the mathematical property that if num has a divisor greater than its square root, it must also have a divisor smaller than the square root, thereby ensuring all potential divisors are checked. If any divisor is found (indicated by num % i == 0), the function returns false.

Main Program:
In the main function, the user is prompted to enter a number. The program then calls isPrime to check if the entered number is prime and outputs the appropriate message based on the function’s return value.