C++ Program to Check Prime Number
Last Updated :
19 May, 2025
A Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Examples:
Input: n = 29
Output: 29 is prime
Explanation: 29 has no divisors other than 1 and 29 itself. Hence, it is a prime number.
Input: n = 15
Output: 15 is NOT prime
Explanation: 15 has divisors other than 1 and 15 (i.e., 3 and 5). Hence, it is not a prime number.
There are many approaches for checking whether the given number is a prime number:
Brute Force Method - O(n) Time
In this approach, we can check whether the number is prime or not by iterating in the range from 1 to n. If there are more than 2 divisor (including 1 and n) then the given number n is not prime, else n is prime. This method is known as trial division method.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 29;
int cnt = 0;
// If number is less than/equal to 1,
// it is not prime
if (n <= 1)
cout << n << " is NOT prime";
else {
// Count the divisors of n
for (int i = 1; i <= n; i++) {
if (n % i == 0)
cnt++;
}
// If n is divisible by more than 2
// numbers then it is not prime
if (cnt > 2)
cout << n << " is NOT prime";
// else it is prime
else
cout << n << " is prime";
}
return 0;
}
The time complexity of the above program is O(n) because we iterate from 1 to n.
Optimized Method - O(√n) Time
In this approach, we don't check all divisors of the given number, we only check divisors up to the square root of the number because, "The smallest factor of a number greater than one cannot be greater than the square root of that number."
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 29;
int cnt = 0;
// If number is less than/equal to 1,
// it is not prime
if (n <= 1)
cout << n << " is NOT prime";
else {
// Count the divisors of n
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
cnt++;
}
// If n is divisible by more than 2
// numbers then it is not prime
if (cnt > 0)
cout << n << " is NOT prime";
// else it is prime
else
cout << n << " is prime";
}
return 0;
}
The time complexity of the above program is O(√n) because we iterate only √n times.
We can further optimize the above approach by skipping all even numbers greater than 2. Since the only even prime number is 2, we can skip all even numbers between 3 and √n.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 29;
int cnt = 0;
// If number is less than/equal
// to 1 and number is even accept 2
// then it is not prime
if (n <= 1 || ((n > 2) && (n%2 == 0)))
cout << n << " is NOT prime";
else {
if (n == 2) {
cout << n << " is prime";
return 0;
} else {
// Check how many numbers divide
// n in the range 2 to sqrt(n)
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
cnt++;
}
// if cnt is greater than 0,
// then n is not prime
if (cnt > 0)
cout << n << " is NOT prime";
// else n is prime
else
cout << n << " is prime";
}
}
return 0;
}
Similar Reads
C++ Program To Check Whether a Number is Prime or not Given a positive integer N. The task is to write a C++ program to check if the number is prime or not. Definition: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ....} The idea to solve this
3 min read
C++ Program to Print Prime Numbers from 1 to n A prime number is a natural number that has only two divisors, which are 1 and itself. In this article, we will learn how to print all the prime numbers from 1 to n in C++.ExamplesInput: n = 10Output: 2, 3, 5, 7Explanation: As 2, 3, 5, 7 are the only prime numbers between 1 to 10.Input: n = 5Output:
5 min read
C Program To Find Prime Numbers Between Given Range A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. In this article, we will learn how to find all the prime numbers between the given range.ExampleInput: l = 10, r = 30Output: 11 13 17 19Explanat
6 min read
C++ Program To Find Prime Numbers Between Given Interval A prime number is defined as a natural number greater than 1 and is divisible by only 1 and itself. In other words, the prime number is a positive integer greater than 1 that has exactly two factors, 1 and the number itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 . . .Note: 1
7 min read
C++ Program To Check If a Prime Number Can Be Expressed as Sum of Two Prime Numbers Given a prime number N  . The task is to check if it is possible to express N  as sum of two separate prime numbers.Note: The range of N is less than 108. Examples: Input: N = 13 Output: Yes Explanation: The number 13 can be written as 11 + 2, here 11 and 2 are both prime. Input: N = 11 Output: No
2 min read