Learning how to check if a number is prime is one of the most common exercises for C programming beginners. A prime number is a number that is greater than 1 and divisible only by 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are all prime numbers, while 4, 6, 8, 9, and 12 are not. Writing a C program to check if a number is prime introduces you to the use of loops, conditional statements, and mathematical logic in programming.

with hands-on learning.
get the skills and confidence to land your next move.
This exercise is useful because it combines theory with practice. On one side, you learn the definition of a prime number in mathematics. On the other side, you convert that logic into a working C program that a computer can run. This helps you practice breaking down problems into steps, implementing them in code, and testing different inputs to confirm your program works correctly. By the end of this tutorial, you will know how to write a complete program to check for prime numbers and understand the reasoning behind every line of code.
Understanding the Logic of Prime Numbers
Before writing code, let us think about what makes a number prime. If you take any number greater than 1, it is prime if no other number can divide it except 1 and itself. For example, if we test the number 7, we can see that it cannot be divided evenly by 2, 3, 4, 5, or 6. Since the only divisors are 1 and 7, we call it a prime number. On the other hand, if we test the number 12, we see that it can be divided evenly by 2, 3, 4, and 6. That means it is not prime.
The program we write in C will follow this same logic. We will ask the user to enter a number, then we will check if it can be divided by any smaller numbers. If no such divisor exists, the number is prime. Otherwise, it is composite.
C Program to Check if a Number is Prime Using a Loop
The most common way to check for a prime number is to use a for
loop that tests divisibility. Here is the full C program:
#include <stdio.h>
int main() {
int num, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime == 1) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
Let us carefully understand how this program works. The first line #include <stdio.h>
includes the standard input-output library, which gives us access to functions like printf()
and scanf()
. Inside the main()
function, we declare three variables: num
to store the user’s input, i
as a loop counter, and isPrime
as a flag to store whether the number is prime (1 means prime, 0 means not prime).
The program first asks the user to enter a number using printf()
and reads it with scanf()
. If the number is less than or equal to 1, we immediately set isPrime
to 0 because numbers like 0, 1, and negative numbers are not prime. Otherwise, we enter a for
loop where i
runs from 2 up to num / 2
. The reason we check only up to num / 2
is that no number greater than half of num
can divide it evenly except itself. Inside the loop, we check if (num % i == 0)
. If this condition is true, it means the number is divisible by i
and therefore not prime. We set isPrime = 0
and break the loop.
Finally, we use an if
statement to print whether the number is prime based on the value of isPrime
. If it is 1, we print that the number is prime. Otherwise, we print that it is not prime.
Optimizing the Prime Number Check
The above program works, but it can be improved. Instead of checking divisibility up to num / 2
, we can check only up to the square root of num
. This is because if a number has a divisor greater than its square root, the corresponding divisor would already have been found below the square root. This makes the program faster for large numbers.
Here is an improved version:
#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;
} else {
for (i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime == 1) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
In this version, we include the math.h
library so that we can use the sqrt()
function. The loop now runs from 2 up to the square root of the number. For large inputs, this reduces the number of iterations and makes the program more efficient.
Common Mistakes When Writing a Prime Number Program
Beginners often run into these mistakes when checking for prime numbers:
- Forgetting that numbers less than or equal to 1 are not prime, which can lead to incorrectly classifying 0 or 1 as prime.
- Not resetting or updating the
isPrime
flag properly, which may produce wrong results. - Failing to break out of the loop after finding a divisor, causing unnecessary checks even though the number is already known to be non-prime.
- Misunderstanding loop conditions. Using
i <= num
instead ofi <= num / 2
ori <= sqrt(num)
will always find the number divisible by itself, leading the program to incorrectly mark every number as not prime.
To avoid these errors, always handle small numbers separately, reset flags correctly, use break
when a divisor is found, and pay close attention to loop boundaries.
FAQs
Q1: Can negative numbers be prime?
No, prime numbers are defined only for positive integers greater than 1. Negative numbers and zero are not prime.
Q2: Why do we only check divisibility up to the square root?
If a number has a factor larger than its square root, the corresponding smaller factor must exist below the square root. Therefore, checking up to the square root is enough to determine if the number is prime.
Q3: Is 2 a prime number?
Yes, 2 is the smallest prime number. It is divisible only by 1 and 2. It is also the only even prime number.
Q4: Can I use while loops instead of for loops?
Yes, you can use a while
loop instead of a for
loop to test divisibility. The logic remains the same, and both approaches will work.
Conclusion
Writing a C program to check if a number is prime is an excellent exercise for beginners. It teaches you how to use loops, conditional statements, and mathematical logic together. By practicing this program, you will strengthen your understanding of divisibility, loop conditions, and program structure in C. We looked at both the simple method and the optimized method using the square root. With these techniques, you are well prepared to handle more advanced problems in number theory and algorithm design using C.
References & Additional Resources
- Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
- Wikipedia: Prime Number – Mathematical definition and properties of prime numbers.
- GeeksforGeeks: Prime Number in C – Additional code examples and variations.
- cplusplus.com: math.h – Documentation for mathematical functions in C.
- Stack Overflow: Efficient Prime Checking – Discussion on prime number algorithms.