Finding all integers in a range that are divisible by a given number is a classic loop exercise. The key tool is the modulo operator % — if i % 7 == 0, then i is exactly divisible by 7 with no remainder. This post shows two programs: one for the classic 100–200 range, and a flexible version where the user supplies the range and divisor.
Method 1 — Numbers from 100 to 200 Divisible by 7
#include <stdio.h>
int main(void)
{
int i, count = 0, sum = 0;
printf("Numbers from 100 to 200 divisible by 7:\n");
for (i = 100; i <= 200; i++) {
if (i % 7 == 0) {
printf("%d ", i);
sum += i;
count++;
}
}
printf("\n\nCount : %d\n", count);
printf("Sum : %d\n", sum);
return 0;
}
Output
Numbers from 100 to 200 divisible by 7:
105 112 119 126 133 140 147 154 161 168 175 182 189 196
Count : 14
Sum : 2107
Method 2 — User-Supplied Range and Divisor
The same logic works for any range and any divisor. This version asks the user for all three values and guards against division by zero.
#include <stdio.h>
int main(void)
{
int start, end, divisor, i, count = 0, sum = 0;
printf("Enter start, end, and divisor: ");
scanf("%d %d %d", &start, &end, &divisor);
if (divisor == 0) {
printf("Error: divisor cannot be zero.\n");
return 1;
}
printf("\nNumbers from %d to %d divisible by %d:\n",
start, end, divisor);
for (i = start; i <= end; i++) {
if (i % divisor == 0) {
printf("%d ", i);
sum += i;
count++;
}
}
if (count == 0)
printf("(none)");
printf("\n\nCount : %d\n", count);
printf("Sum : %d\n", sum);
return 0;
}
More Sample Runs
Enter start, end, and divisor: 1 50 5
Numbers from 1 to 50 divisible by 5:
5 10 15 20 25 30 35 40 45 50
Count : 10
Sum : 275
Enter start, end, and divisor: 1 10 11
Numbers from 1 to 10 divisible by 11:
(none)
Count : 0
Sum : 0
How to Compile and Run
gcc -ansi -Wall -Wextra -o divisible divisible.c
./divisible
Code Explanation
The modulo operator % returns the remainder after integer division. 105 % 7 is 0 because 105 = 7 × 15 exactly. 106 % 7 is 1 because 106 = 7 × 15 + 1. So i % 7 == 0 is the precise test for divisibility.
Why the zero check? The modulo operator with a zero right-hand operand is undefined behaviour in C — it corresponds to dividing by zero. Method 2 checks for this before entering the loop and exits with an error code of 1, which signals failure to the shell.
Counting vs listing: count++ inside the if block increments a counter each time a match is found. sum += i accumulates the running total. Both finish in a single pass through the loop — no second iteration needed.
What This Program Teaches
- How the modulo operator
%tests for exact divisibility - How to count and sum matching values inside a loop with a single pass
- How to handle an invalid input (zero divisor) with an early return
- How to generalise a hardcoded solution into a user-driven one
Related C Programs
📖 Learning C from a book? The C Programming Language by K&R (Amazon.in) · Amazon.com
Preparing for a C interview? Practice with the C Programming Quiz — 150+ questions, free on Android.