Table of contents
1.
Introduction
2.
1. Check for a Prime Number using the Simple Method
2.1.
C
3.
2. Check for Prime Number using sqrt(n)
3.1.
C
4.
FAQs
4.1.
What is a prime number?
4.2.
What is the logic of finding prime numbers using the C language?
4.3.
Why is 2 a prime number?
5.
Conclusion
Last Updated: Aug 27, 2024
Easy

Prime Number Program using C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A prime number is defined as a natural number greater than 1 that has no positive divisors other than 1 and itself. To determine if a number n is prime, one must check divisibility from 2 to n−1. If n is divisible by any of these numbers, it is not prime. The C programming language offers various looping constructs that facilitate this prime-checking process efficiently. So, in this article, we will see two easy methods, the first one is the “simple method” and another one is the “sqrt method” to check if the number is prime or not.

Also see: C Static Function, and Tribonacci Series

1. Check for a Prime Number using the Simple Method

 

To check if a number is prime with the help of a simple method, you just need to loop through all integers from 2 to n−1 and check if any of these integers divide n without leaving a remainder. If you find such a divisor, n is not a prime number. If no divisors are found, n is prime.

Let's see an example in C showing this method:

  • C

C

#include <stdio.h>

int isPrime(int n) {
if (n <= 1) return 0; // 0 and 1 are not prime numbers
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return 0; // n is divisible by some number other than 1 and itself
}
}
return 1; // n is a prime number
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

return 0;
}
You can also try this code with Online C Compiler
Run Code

In this program, the isPrime function checks each number from 2 to n−1. If any number divides n evenly, it returns 0, indicating the number is not prime. Otherwise, it returns 1, indicating the number is prime. The main function prompts the user to input a number, checks its primality, and displays the result.

2. Check for Prime Number using sqrt(n)

An optimized method to check if a number n is prime is to only test divisibility up to the square root of n. This is because if n has a divisor greater than its square root, the corresponding co-divisor will be less than the square root. Thus, it’s sufficient to check divisibility only up to n.

For example : 

  • C

C

#include <stdio.h>
#include <math.h> // Include to use sqrt function

int isPrime(int n) {
if (n <= 1) return 0; // 0 and 1 are not prime numbers
if (n <= 3) return 1; // 2 and 3 are prime numbers

if (n % 2 == 0 || n % 3 == 0) return 0; // Quick check for divisibility by 2 or 3

for (int i = 5; i <= sqrt(n); i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return 0; // n is divisible by some number other than 1 and itself
}
}
return 1; // n is a prime number
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

return 0;
}
You can also try this code with Online C Compiler
Run Code

In this program, the isPrime function first eliminates numbers less than 2 and any even numbers greater than 2. It then checks for divisibility using numbers from 5 up to the square root of n, incrementing by 6 each time to skip even numbers and multiple of 3 (since these are already checked). If no divisors are found within this range, n is determined to be prime. This approach significantly reduces the number of checks needed, especially for large numbers.

FAQs

What is a prime number?

A number n is called a prime number if it is only divisible by 1 and the number itself. Prime numbers are more than the number 1 and have precisely two factors: 1 and the number itself.
 

What is the logic of finding prime numbers using the C language?

To check whether a number n is prime or not, we will have to divide n by each number from 2 to n-1. If n is divisible by any number, it is not a prime number, and if no number can divide n, then n is prime. C language provides various looping statements through which our task of checking whether a number is prime or not can be carried out smoothly. 
 

Why is 2 a prime number?

2 is the only even prime number that exists because it has only two factors 1 and itself.

Conclusion

In this blog, we discussed about two easy-to-learn approaches to check the prime number. First, we discussed the “Simple method” which is very to understand as we just have to use the loop in C, and then we use the “Sqrt method” of C, this process significantly reduces the checks needed, which is very useful to save the CPU time for the large numbers.

Live masterclass