The Wayback Machine - https://web.archive.org/web/20240721112926/https://www.geeksforgeeks.org/c-program-to-check-whether-a-number-is-prime-or-not/
Open In App

Prime Number Program in C

Last Updated : 02 Jul, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

A prime number is a natural number greater than 1 that is completely divisible only by 1 and itself. For example, 2, 3, 5, 7, 11, etc. are the first few prime numbers.

In this article, we will explore a few prime number programs in C to check whether the given number is a prime number or not.

prime numbers in c

We can check if a number is prime or not in C using two methods:

  1. Simple Method
  2. Using sqrt(N)

1. Check for Prime Number using Simple Method

In this method, we will check if any number between 2 to (N/2) can divide N completely. If such a number exists, it means that the number N is not a prime number as it is divisible by a number other than 1 and itself.

Note: We take (N/2) as the upper limit because there are no numbers between (N/2) and N that can divide N completely.

C Program to Check Prime Number Using Simple Approach

C
// C Program to check for prime number using Simple Approach
#include <stdio.h>

// Function to check prime number
void checkPrime(int N)
{
    // initially, flag is set to true or 1
    int flag = 1;

    // loop to iterate through 2 to N/2
    for (int i = 2; i <= N / 2; i++) {

        // if N is perfectly divisible by i
        // flag is set to 0 i.e false
        if (N % i == 0) {
            flag = 0;
            break;
        }
    }

    if (flag) {
        printf("The number %d is a Prime Number\n", N);
    }
    else {
        printf("The number %d is not a Prime Number\n", N);
    }

    return;
}

// driver code
int main()
{
    int N = 546;

    checkPrime(N);

    return 0;
}

Output
The number 546 is not a Prime Number

Time Complexity: O(n), where n is the given number.

Space Complexity: O(1)

2. Check for Prime Number using sqrt(n)

In this method, we use a mathematical property which states that,

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

Using this, we can reduce the numbers to be checked from (N/2) to sqrt(N) making it much more efficient than the simple method.

Algorithm

  1. Initialize an integer named flag with 1.
  2. Check the divisibility of N with every number from 1 to sqrt(N).
  3. If any number divides N completely, it means that N is not prime. We set the flag to 0 and stop the loop immediately.
  4. If we did not find any number between 2 and sqrt(N) which divides N then it means that N is prime and the flag remains 1.

C Program to Check Prime Number Using sqrt(N)

C
// C program to check if a number is prime using sqrt(n)
#include <math.h>
#include <stdio.h>
int main()
{
    int N = 17;
    int flag = 1;

    double sqroot = sqrt(N);

    // Iterate from 2 to sqrt(n)
    for (int i = 2; i <= sqroot; i++) {

        // If n is divisible by any number between 2 and
        // sqrt(n), it is not prime
        if (N % i == 0) {
            flag = 0;
            break;
        }
    }

    if (flag) {
        printf("%d is a prime number", N);
    }
    else {
        printf("%d is not a prime number", N);
    }

    return 0;
}

Output
17 is a prime number

Time Complexity: O(n1/2), where n is the given number.
Auxiliary Space: O(1)

There are a few more methods to check for prime numbers which are shown in the article – Prime Numbers



Similar Reads

C Program for Find largest prime factor of a number
Given a positive integer 'n'( 1 &lt;= n &lt;= 1015). Find the largest prime factor of a number. Input: 6 Output: 3 Explanation Prime factor of 6 are- 2, 3 Largest of them is '3' Input: 15 Output: 5Method 1: The approach is simple, just factorise the given number by dividing it with the divisor of a number and keep updating the maximum prime factor.
3 min read
C Program for efficiently print all prime factors of a given number
Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be "2 2 3". And if the input number is 315, then output should be "3 3 5 7". First Approach: Following are the steps to find all prime factors. 1) While n is divisible by 2, print 2 and divide n by 2. 2) After st
5 min read
C/C++ Program to find Product of unique prime factors of a number
Given a number n, we need to find the product of all of its unique prime factors. Prime factors: It is basically a factor of the number that is a prime number itself. Examples: Input: num = 10 Output: Product is 10 Explanation: Here, the input number is 10 having only 2 prime factors and they are 5 and 2. And hence their product is 10. Input : num
3 min read
C Program To Check Prime Number By Creating a Function
Prime numbers have only 2 factors, 1 and themselves. For example, 2,3, 5, 7, 11,... are the first 5 prime numbers. Here we will build a C program to check prime numbers by creating a function. Approaches to Check Prime number using 3 different approaches: Using for loop for prime number.Using for loop for not prime number.Using while loop for prime
3 min read
C Program to Check Whether a Number Can Be Express as Sum of Two Prime Numbers
Prime numbers are numbers that have only 2 factors, 1 and themselves. For example, 2, 3, 5, 7, 11, etc are some of the first prime numbers. Here we will see whether a number can be expressed as the sum of two prime numbers using a C program. Example Input: 7Output: YesExplanation: 7 can be expressed as sum of 2 and 5 which are prime Input: 11Output
2 min read
C++ Program to check Prime Number
A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 5 is prime but 10 is not prime. In this article, we will learn how to write a C++ program to check whether a number is prime or not. Algorithm to Check Prime Numbers in C++Run a loop from 2 to n/2 using a variable i.Inside the loop, check
2 min read
C Program to Display Prime Numbers Between Intervals
Given two numbers a and b as interval range, the task is to find the prime numbers in between this interval. Examples: Input: a = 1, b = 10 Output: 2, 3, 5, 7 Input: a = 10, b = 20 Output: 11, 13, 17, 19Approach 1: In the below program, the range of numbers is taken as input and stored in the variables 'a' and 'b'. Then using for-loop, the numbers
6 min read
C Program to Display Prime Numbers Between Two Intervals Using Functions
Prime numbers have only 2 factors, 1 and themselves. For example, 2,3, 5, 7, 9,... are the first 5 prime numbers. Here we will build a C program to display prime numbers between two intervals using functions using 2 approaches, for loop and while loop. Example Input: num1 = 2, num2 = 10 Output: Prime numbers between 2 and 10 are: 2 3 5 7 Explanatio
3 min read
C Program to Print Prime Numbers From 1 to N
Prime numbers are positive integers greater than 1 that have no divisors other than 1 and themselves. In this article, we will learn to generate and display all prime numbers from 1 to N in C programming language.  AlgorithmCheck every number from 1 to N whether it is prime by using isPrime() function.In isPrime() Function,Iterate from 2 to n/2 and
2 min read
Program to find Prime Numbers Between given Interval
Given two numbers  a and b as interval range, the task is to find the prime numbers in between this interval. Examples:  Input : a = 1, b = 10Output : 2, 3, 5, 7Input : a = 10, b = 20Output : 11, 13, 17, 19In the below program, the range of numbers is taken as input and stored in the variables 'a' and 'b'. Then using a for-loop, the numbers between
15+ min read
Article Tags :
three90RightbarBannerImg