Checking whether a number is a prime number is one of the most fundamental problems in programming and mathematics. It is frequently asked in coding interviews, academic exams, and beginner programming exercises. In this detailed guide, you will learn how to write a C program to check prime number using different approaches, understand the core logic behind prime numbers, and explore real-world use cases.
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The most efficient C program to check for a prime number involves checking divisibility only up to the square root of the input number (√n).
This article is designed for beginners to intermediate learners and follows best practices for clarity, readability, and practical understanding.
A prime number is a natural number greater than 1 that has exactly two distinct positive divisors:
If a number has more than two factors, it is called a composite number.
Understanding how to check prime numbers in C helps build a strong foundation in:
The basic idea behind checking whether a number is prime is:
| Step | Description |
|---|---|
| 1 | Read the input number |
| 2 | Check if number ≤ 1 |
| 3 | Test divisibility using a loop |
| 4 | Print whether the number is prime or not |
This is the most straightforward approach using a loop.
#include <stdio.h> int main() { int num, i, flag = 0; printf("Enter a number: "); scanf("%d", &num); if (num <= 1) { flag = 1; } for (i = 2; i < num; i++) { if (num % i == 0) { flag = 1; break; } } if (flag == 0) printf("%d is a prime number.", num); else printf("%d is not a prime number.", num); return 0; }
The above approach works but is not efficient for large numbers. A number only needs to be checked up to its square root.
If a number n has a factor greater than √n, it must also have a factor smaller than √n.
#include <stdio.h> #include <math.h> int main() { int num, i, isPrime = 1; printf("Enter a number: "); scanf("%d", &num); if (num <= 1) { isPrime = 0; } for (i = 2; i <= sqrt(num); i++) { if (num % i == 0) { isPrime = 0; break; } } if (isPrime) printf("%d is a prime number.", num); else printf("%d is not a prime number.", num); return 0; }
Using functions improves code reusability and readability.
#include <stdio.h> int isPrime(int num) { int i; if (num <= 1) return 0; for (i = 2; i <= num / 2; i++) { if (num % i == 0) return 0; } return 1; } int main() { int number; printf("Enter a number: "); scanf("%d", &number); if (isPrime(number)) printf("%d is a prime number.", number); else printf("%d is not a prime number.", number); return 0; }
Learning how to write a C program to check prime number is an essential step in mastering programming fundamentals. From basic logic to optimized approaches and function-based implementations, this topic helps strengthen your understanding of loops, conditions, and algorithm efficiency.
By practicing these examples and understanding the underlying logic, you will be better prepared for exams, interviews, and real-world programming challenges.
No, 1 is not a prime number because it has only one factor.
All other even numbers are divisible by 2, making them composite.
The square root method is the most efficient and commonly used.
No, prime numbers are defined only for natural numbers greater than 1.
Yes, prime numbers are widely used in cryptography, cybersecurity, and mathematical computations.
Copyrights © 2024 letsupdateskills All rights reserved