C Program to Check Whether a Number is Prime or Not
In this article, we will discuss prime number programs in C and the different approaches they use to find whether the number is prime or not.
What is a Prime Number?
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Based on this property there are many different methods to verify whether the number is prime or not. The first few prime numbers are {2, 3, 5, 7, 11}.

For Example,
Prime Numbers
5,7,11,13,17,19....
Non-Prime Numbers
2,6,9,15,21...
Programs to Check for Prime Numbers in C
There are multiple prime number programs in C each of which uses different approaches or methods to identify whether a number is prime or not. Some of the methods using which we can implement to check prime numbers are:
- Naive Approach
- Using sqrt(n)
- Using Most Efficient sqrt(N)
- Using Wilson’s Theorem
1. Naive Approach to check Prime number in C
We know that prime numbers are only divisible by 1 and itself. Based on this property, we design the logic of our program which is as follows:
- We check the divisibility of the given number N with all the numbers starting from 2 to (N/2).
- If it is completely divisible by any number i.e. remainder is zero after division, then the number is not a prime number.
- If it is not completely divisible by a number between 2 and (N/2), then the number is a prime number.
C
// C Program to check for prime number using Naive 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; } |
The number 546 is not a Prime Number
Time Complexity: O(n), where n is the magnitude of the number.
Space Complexity: O(1), as the space used does not depend upon the number.
2. Program to Check Prime Number using sqrt(N)
This method is the best (but not optimal) method to check prime numbers. In mathematics, the smallest and greater than one factor of a number cannot be more than the square root of that number. We use this property to design the logic of the program which is as follows:
- The idea to solve this problem is to iterate through all the numbers starting from 2 to sqrt(N) using a for loop and for every number check if it divides N.
- If any number divides N completely, it means that N is not prime. We set the flag to 0 and stop the loop immediately.
- 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
// 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; } |
17 is a prime number
Time Complexity: O(n1/2), as we need to iterate only till n1/2
Auxiliary Space: O(1), as the space doesn’t depend upon the given number.
3. Program to Check Prime Number using Most Efficient sqrt(N)
This method is the optimal one to check if a number is prime or not.
Approach:
The approach behind this solution is:
- We will first deal with a few numbers such as 1, 2, 3 in a case.
- Then we deal with the numbers which are divisible by 2 and 3 in a separate case.
- For remaining numbers:
- Iterate from 5 to sqrt(n) and check for each iteration whether (that value) or (that value + 2) divides n or not.
- If we find any number that divides, we return false.
- Again increment the value by 6 [because any prime can be expressed as 6n+1 or 6n-1].
- Iterate from 5 to sqrt(n) and check for each iteration whether (that value) or (that value + 2) divides n or not.
- If any number can’t divide the number, then we return true.
Below is the implementation of the above approach:
C
// C program to check if a number is prime using most efficient sqrt(n) #include <math.h> #include <stdio.h> // Function check whether a number // is prime or not int isPrime( int n) { // Check if n=1 or n=0 if (n <= 1) return 0; // Check if n=2 or n=3 if (n == 2 || n == 3) return 1; // Check whether n is divisible by 2 or 3 if (n % 2 == 0 || n % 3 == 0) return 0; // Check from 5 to square root of n // Iterate i by (i+6) for ( int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return 0; return 1; } // Driver Code int main() { int N = 17; if (isPrime(N)) printf ( "%d is a prime number" , N); else printf ( "%d is not a prime number" , N); return 0; } // This code is contributed by Susobhan Akhuli |
17 is a prime number
Time complexity: O(sqrt(N)), Where n is the number.
Auxiliary space: O(1)
4. Program to Check Prime Numbers using Wilson’s Theorem
Wilson’s Theorem states that the prime number p completely divides ((p – 1)! + 1), i.e. ((p – 1)! + ) % p == 0. We will use this property to design our logic which is as follows:
- We will evaluate the (N-1)! + 1, where N is the given number.
- Then we will check the divisibility of (N – 1)! + 1 with N, i.e. ((N – 1)! + ) % N == 0.
- If the remainder is zero, then N is a prime number.
- If the remainder is not zero, then N is not prime.
C
#include <stdio.h> // utility function to evaluate factorial long long int getFactorial( int n) { long long int f = 1; for ( int i = 2; i <= n; i++) { f *= i; } return f; } // function to check prime number void checkPrime( int N) { long long fact = getFactorial(N - 1); if (fact % N == N - 1) { printf ( "The number %d is a prime number." , N); } else { printf ( "The number %d is not a prime number." , N); } return ; } // driver code int main() { int N = 17; checkPrime(N); return 0; } |
The number 17 is a prime number.
Time Complexity: O(N), due to the time complexity for calculating the factorial of N.
Auxiliary Space: O(1), as the space required is independent of the value of N.
Please Login to comment...