Open In App

C++ Program to Check Prime Number

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
29 is prime

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;
}

Output
29 is prime

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;
}

Output
29 is prime

Next Article
Practice Tags :

Similar Reads