Prime Number Program in C (original) (raw)

Last Updated : 11 Jul, 2025

A **prime number is a natural number greater than 1 and is completely divisible only by 1 and itself. In this article, we will learn how to check whether the given number is a prime number or not in C.

**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.

We can check whether a number is prime using various approaches:

Table of Content

Brute Force Method - O(n) Time

We can check whether the number is **prime or not by iterating in the range from 1 to **n using loops. We will count the number of divisors. 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.

**Example:

C `

#include <stdbool.h> #include <stdio.h>

int main() { int n = 29; int cnt = 0;

// If number is less than/equal to 1,
// it is not prime
if (n <= 1)
    printf("%d is NOT prime", n);
else {

    // Count the all divisors of 
    // given number
    for (int i = 1; i <= n; i++) {

        // Check n is divided by
        // i or not
        if (n % i == 0)
            cnt++;
    }

    // If n is divisible by more than 2 numbers
    // then it is not prime
    if (cnt > 2)
        printf("%d is NOT prime", n);

    // else it is prime
    else
        printf("%d is prime", n);
}
return 0;

}

`

The time complexity of the above program is **O(n) because we iterate from 1 to n.

Optimized Approach - **O(√n) Time

To optimize the above approach, we use a mathematical property which states that,

**The smallest factor of a number greater than one cannot be greater than the square root of that number.

Using this, we can reduce the numbers to be checked from N to √N making it much more efficient than above approach.

**Example:

C `

#include <math.h> #include <stdbool.h> #include <stdio.h>

int main() { int n = 29; int cnt = 0;

// If number is less than/equal to 1,
// it is not prime
if (n <= 1)
    printf("%d is NOT prime", n);
else {

    // Check how many numbers divide n in
    // range 2 to sqrt(n)
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            cnt++;
    }

    // if cnt is greater than 0 then n is
    // not prime
    if (cnt > 0)
        printf("%d is NOT prime", n);

    // else n is prime
    else
        printf("%d is prime", n);
}
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.

**Example:

C `

#include <math.h> #include <stdbool.h> #include <stdio.h>

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)))
    printf("%d is NOT prime", n);
else {

    if(n==2){
        printf("%d is prime", n);
        return 0;
    }else{
        
    // Check how many numbers divide n in
    // 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)
        printf("%d is NOT prime", n);

    // else n is prime
    else
        printf("%d is prime", n);
    }
}

return 0;

}

`