Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Check Whether a Number is Prime in C++



A prime number is a whole number greater than one and the only factors of a prime number should be one and itself. For example, 2, 3, 5, 7, and 11 are prime numbers.

Our task is to write a C++ program that checks if a given number is prime or not. We will cover different approaches to solve this problem.

We'll cover the following methods for checking whether a number is prime or not:

Checking Prime Number Using Loop

In this approach, we use a loop to check if the number is divisible by any integer from 2 to n-1. If the number is divisible by any of these integers, it is not prime. If no divisors are found, the number is prime.

Example:

Here's a complete C++ program where we use a loop to check if the number is prime or not.

#include <iostream>
using namespace std;

bool isPrime(int n) {
    if (n <= 1) return false; // Numbers less than or equal to 1 are not prime
    for (int i = 2; i < n; i++) {
        if (n % i == 0) return false; // If divisible by i, it's not prime
    }
    return true;
}

int main() {
    int num = 29;
    if (isPrime(num)) {
        cout << num << " is a prime number." << endl;
    } else {
        cout << num << " is not a prime number." << endl;
    }
    return 0;
}

The output below shows that 29 is a prime number since it has no divisors other than 1 and itself.

29 is a prime number.

Time Complexity: O(n) because we check divisibility for all numbers up to n.

Space Complexity: O(1) because only a few variables are used for the loop and check.

Checking Prime Number Using Square Root

In this approach, we check divisibility only up to the square root of the number. If a number has a factor larger than its square root, then a smaller matching factor would have already been checked.

Example

Here's a complete C++ program that checks if a number is prime using the square root method. It loops from 2 to sqrt(n) and checks for divisibility. If no divisors are found, the number is confirmed as prime.

#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int n) {
    if (n <= 1) return false;  // Numbers less than or equal to 1 are not prime
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) return false;  // If divisible by i, it's not prime
    }
    return true;  // No divisors found, so it's prime
}

int main() {
    int num1 = 29;  // Input number
    int num2 = 16;  //second Input
    if (isPrime(num1)) {
        cout << "The number " << num1 << " is prime." << endl;
    } else {
        cout << "The number " << num1 << " is not prime." << endl;
    }
    
    if (isPrime(num2)) {
        cout << "The number " << num2 << " is prime." << endl;
    } else {
        cout << "The number " << num2 << " is not prime." << endl;
    }
    return 0;
}

The output of the above program shows that 29 is prime and 16 is not prime.

The number 29 is prime.
The number 16 is not prime.

Time Complexity: O(sqrt(n)), because we only check numbers up to the square root of n.

Space Complexity: O(1), since we only use a fixed amount of space.

Checking Prime Number Using Optimized Method with Even Check

In this approach, we first check if the number is 2, as it is the only even prime number. For other numbers, we skip even numbers greater than 2 and check divisibility only for odd numbers up to the square root of the number.

Example

Here's a complete C++ program where we use the optimized even check method to check if a number is prime or not.

#include <iostream>
using namespace std;

bool isPrime(int n) {
    if (n <= 1) return false; // Numbers less than or equal to 1 are not prime
    if (n == 2) return true;  // 2 is a prime number
    if (n % 2 == 0) return false; // Even numbers greater than 2 are not prime
    for (int i = 3; i * i <= n; i += 2) { // Check only odd numbers
        if (n % i == 0) return false; // If divisible by i, it's not prime
    }
    return true;
}

int main() {
    int num1 = 29;  // Prime number
    int num2 = 16;  // Non-prime number

    if (isPrime(num1)) {
        cout << "The number " << num1 << " is prime." << endl;
    } else {
        cout << "The number " << num1 << " is not prime." << endl;
    }

    if (isPrime(num2)) {
        cout << "The number " << num2 << " is prime." << endl;
    } else {
        cout << "The number " << num2 << " is not prime." << endl;
    }
    return 0;
}

The output of the above program shows that 29 is prime, while 16 is not.

The number 29 is prime.
The number 16 is not prime.

Time Complexity: O(sqrt(n)) because we only check divisibility for odd numbers up to the square root of n .

Space Complexity: O(1), using a constant amount of space.

Updated on: 2025-05-13T16:59:42+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements