C Program to Print Prime Numbers in a Range
C Program to Print Prime Numbers in a Range
A school number challenge asks for prime labels in an inclusive interval. Test each candidate without treating one as a prime.
A prime integer is at least two and has no positive divisors other than one and itself. A factor test only needs candidates through its square-root boundary.
Before this exercise, review loops and stopping conditions and functions and parameters. The worked program below applies those ideas to this specific problem.
Explain
Explain why primes in an interval uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Print primes between 14 and 20, including both interval endpoints.
Primes in an Interval: follow the data
Given data
A school number challenge asks for prime labels in an inclusive interval. Test each candidate without treating one as a prime.
Rule
A prime integer is at least two and has no positive divisors other than one and itself. A factor test only needs candidates through its square-root boundary.
Observed result
11 13 17 19 23 29
Understand the operation
If a composite has factors a and b, at least one is no greater than its square root. The condition divisor <= value / divisor reaches that boundary without computing divisor * divisor, which could overflow for large values.
Return false immediately when a divisor leaves remainder zero. Return true only after every required divisor was tried. The outer loop enumerates candidates; the helper owns the prime decision for one candidate.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
static int is_prime(int value) {
if (value < 2) return 0;
for (int divisor = 2; divisor <= value / divisor; divisor++) {
if (value % divisor == 0) return 0;
}
return 1;
}
int main(void) {
for (int n = 10; n <= 30; n++) {
if (is_prime(n)) printf("%d ", n);
}
putchar('\n');
return 0;
}
Expected output
11 13 17 19 23 29
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
static int is_prime(int value) {
if (value < 2) return 0;
for (int divisor = 2; divisor <= value / divisor; divisor++) {
if (value % divisor == 0) return 0;
}
return 1;
}
int main(void) {
for (int n = 10; n <= 30; n++) {
if (is_prime(n)) printf("%d ", n);
}
putchar('\n');
return 0;
}
The candidates run from 10 through 30, including the endpoints.
Each tested divisor is a possible witness of compositeness. A successful remainder-zero test stops that candidate’s search; finding no witness within the bound confirms it.
Follow the worked calculation
Start with the sample
The candidates run from 10 through 30, including the endpoints.
Apply the rule
Each tested divisor is a possible witness of compositeness. A successful remainder-zero test stops that candidate’s search; finding no witness within the bound confirms it.
Check the result
11 13 17 19 23 29
11 13 17 19 23 29
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
static int is_prime(int value) {
if (value < 2) return 0;
for (int divisor = 2; divisor <= value / divisor; divisor++) {
if (value % divisor == 0) return 0;
}
return 1;
}
int main(void) {
for (int n = 10; n <= 30; n++) {
if (is_prime(n)) printf("%d ", n);
}
putchar('\n');
return 0;
}
Print primes between 14 and 20, including both interval endpoints.
Worked answer
Fourteen, fifteen, sixteen, eighteen and twenty each have a small factor; seventeen and nineteen do not. The output contains only 17 and 19.
C17 · complete program
#include <stdio.h>
static int is_prime(int value) {
if (value < 2) return 0;
for (int divisor = 2; divisor <= value / divisor; divisor++) {
if (value % divisor == 0) return 0;
}
return 1;
}
int main(void) {
for (int n = 14; n <= 20; n++) {
if (is_prime(n)) printf("%d ", n);
}
putchar('\n');
return 0;
}
Expected output
17 19
Run in C compiler →Recognize the important boundaries
Zero, one and negative integers are not primes. Equality at the divisor boundary matters: a square such as 49 must test divisor seven.
This trial-division helper is useful for small classroom values; listing very large intervals efficiently needs a more suitable algorithm, such as a sieve.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
A prime integer is at least two and has no positive divisors other than one and itself. A factor test only needs candidates through its square-root boundary.
Continue with function contracts.
Continue with all factors.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.