PPS Lab · Experiment 10
Check whether a number is prime
CExpression evaluationJNTUH R22 · CS108ES
Aim
To write a C program that reads a positive integer and checks whether it is a prime number.
Algorithm
- Read the number n.
- If n is less than 2, it is not prime. Print the result and stop.
- Assume n is prime.
- For each i from 2 while i × i ≤ n: if n % i is 0, n has a divisor other than 1 and itself, so mark it as not prime and stop the loop.
- Print whether n is prime or not.
Program
#include <stdio.h>
int main(void) {
int n;
printf("Enter a positive integer: ");
if (scanf("%d", &n) != 1) {
printf("Invalid input\n");
return 1;
}
int is_prime = 1; /* 1 means prime, 0 means not prime */
if (n < 2) {
is_prime = 0;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
is_prime = 0;
break;
}
}
if (is_prime) {
printf("%d is a prime number\n", n);
} else {
printf("%d is not a prime number\n", n);
}
return 0;
}Output
A prime number
Enter a positive integer: 97
97 is a prime numberNot prime (91 = 7 x 13)
Enter a positive integer: 91
91 is not a prime numberThe number 1
Enter a positive integer: 1
1 is not a prime numberRecorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
A prime number is a whole number greater than 1 whose only divisors are 1 and itself. So 0 and 1 are not prime, and 2 is the smallest prime.
It is enough to test divisors up to √n. If n = p × q and both p and q were bigger than √n, their product would be bigger than n. So any number that has a divisor also has one that is at most √n. The condition i * i <= n checks this without using sqrt. For 97 the loop only tries 2 to 9.
Common mistakes
Treating 1 as prime. The loop never runs for n = 1, so without the
n < 2check the program says 1 is prime.Printing "not prime" or "prime" inside the loop. The program then prints a line for every divisor tried. Use a flag and print once after the loop.
Starting the loop at
i = 1. Every number is divisible by 1, so every number would be reported as not prime.Testing every number up to n − 1. It gives the right answer but is slow for large n; testing up to √n is enough.
Viva questions
Try to answer each one aloud before you open it.
What is a prime number?
A whole number greater than 1 that has exactly two divisors, 1 and itself. Examples are 2, 3, 5, 7, 11 and 97.
Is 1 a prime number? Is 2?
1 is not prime because it has only one divisor. 2 is prime, and it is the only even prime number.
Why is it enough to check divisors only up to the square root of n?
If n = p × q, both factors cannot be larger than √n, because then their product would be larger than n. So if n has any divisor other than 1 and n, it has one that is at most √n.
What is the use of break in this program?
As soon as one divisor is found, n is known to be not prime, so
breakleaves the loop without testing the remaining numbers.What is a flag variable?
A variable, here
is_prime, that records whether something has happened. It is set before the loop, changed inside it, and checked after it.