- How the
%operator computes the remainder of integer division - Why
%works only on integers (not ondoubleorfloat) - How the remainder relates to the quotient:
a = b * (a / b) + (a % b) - How negative operands affect the sign of the remainder
- Why
%gives a remainder of0for exact divisibility
The modulus operator
The modulus operator % (often called "mod") returns the remainder after integer division. For a % b, the result is what's left over when you divide a by b. If a = 10 and b = 3, then 10 / 3 = 3 (quotient) with a remainder of 1, so 10 % 3 = 1.
The relationship between division and modulus is captured by the division algorithm: for any integers a and b (with b ≠ 0),
a = b * (a / b) + (a % b)
In words: the dividend equals the divisor times the quotient, plus the remainder. This is the fundamental identity that connects / and % — they're two views of the same division operation. When the remainder is zero, a is an exact multiple of b.
Critical constraint: % works only on integers. Applying % to a double or float is a compile error in C. For floating-point remainder, use fmod() from <math.h> — for example, fmod(7.5, 2.0) returns 1.5.
Sign rules for negative operands: until C99, the sign of a % b was implementation-defined. Since C99, the rule is that a % b has the same sign as a (the dividend). So −7 % 2 = −1, 7 % −2 = 1, and −7 % −2 = −1. In all cases, the remainder's magnitude is less than the divisor's magnitude.
Complete C Program
Here is the program — find the remainder of two integers:
Program: Remainder of two integers
#include <stdio.h>
int main(void) {
int a, b, rem;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
if (b == 0) {
printf("Error: modulus by zero is undefined\n");
return 1;
}
rem = a % b;
printf("Remainder = %d\n", rem);
return 0;
}
A variant that also prints the quotient — useful to verify the division algorithm:
Variant: Print quotient and remainder together
#include <stdio.h>
int main(void) {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
if (b == 0) {
printf("Error: division by zero\n");
return 1;
}
printf("%d / %d = %d (quotient)\n", a, b, a / b);
printf("%d %% %d = %d (remainder)\n", a, b, a % b);
printf("Check: %d = %d * %d + %d\n", a, b, a / b, a % b);
return 0;
}
Note the %% in the second printf — that's the escape sequence for printing a literal % character. Without the doubled percent, printf would misinterpret % as the start of a format specifier.
Program Output
Sample run:
Enter two integers: 10 3
Remainder = 1
10 ÷ 3 = 3 remainder 1, so 10 % 3 = 1. Correct.
Exact divisibility:
Enter two integers: 15 5
Remainder = 0
15 ÷ 5 = 3 remainder 0. When the remainder is zero, the first number is exactly divisible by the second — useful for checking even/odd (n % 2 == 0) or multiples.
Negative dividend:
Enter two integers: -7 2
Remainder = -1
−7 ÷ 2 = −3 remainder −1. The remainder has the sign of the dividend (−7), not the divisor. This is the C99 rule.
The variant output:
Enter two integers: 17 5
17 / 5 = 3 (quotient)
17 % 5 = 2 (remainder)
Check: 17 = 5 * 3 + 2
The check line verifies the division algorithm: 17 = 5 × 3 + 2. Correct.
Modulus by zero:
Enter two integers: 10 0
Error: modulus by zero is undefined
The check prevents a crash. In C, a % 0 is undefined behavior — just like division by zero.
Step-by-Step Explanation
1. Declarations: int a, b, rem; — the modulus operator requires integer operands, so a and b must be int (or another integer type). Using double here would be a compile error.
2. Input: scanf("%d %d", &a, &b) reads two integers. The & is essential.
3. Zero check: if (b == 0) catches modulus by zero. Like division, a % 0 is undefined behavior — you must check before computing.
4. The modulus: rem = a % b computes the remainder. For 10 % 3, the CPU performs integer division and keeps the remainder (1). For 15 % 5, the remainder is 0 (exact division). For −7 % 2, the remainder is −1 (sign matches the dividend −7).
5. Output: printf("Remainder = %d\n", rem) prints the result. For the variant that prints the modulus symbol itself, use %% (escaped percent) in the format string.
6. The division algorithm check: in the variant, a = b * (a / b) + (a % b) is printed explicitly. For 17, 5, this gives 17 = 5 × 3 + 2, confirming that quotient × divisor + remainder = dividend. This is the fundamental relationship between / and %.
% returns the remainder after integer division. It works only on integers (for double, use fmod). The remainder has the same sign as the dividend. Common uses: checking even/odd, extracting digits, cyclic behaviors, hash functions.
Frequently Asked Questions
1. Why can't I use % with double or float?
Because the modulus operator is defined only for integer types in C. Floating-point "remainder" is a different concept (it's the leftover after floating-point division, which involves precision), and it's provided by fmod() in <math.h>. So 10.5 % 3 is a compile error, but fmod(10.5, 3) returns 1.5.
2. Why is −7 % 2 equal to −1, not 1?
Because since C99, the remainder has the same sign as the dividend (the first operand). −7 is negative, so the remainder is −1. In older C dialects, the sign was implementation-defined — the same expression might have returned 1 on some systems. If you need a non-negative remainder (like for cyclic indexing), use ((a % b) + b) % b to normalize it.
3. What happens if I do % by zero?
Undefined behavior — just like division by zero. On most systems, it triggers a SIGFPE signal (crash). Always check the divisor before computing: if (b != 0) rem = a % b;
4. How can I use % to check if a number is even or odd?
Use n % 2. If it's 0, the number is even (divisible by 2). If it's 1 (or −1 for negative numbers), the number is odd. So if (n % 2 == 0) printf("even"); else printf("odd");. This works for any integer, including negatives — but be aware that −5 % 2 = −1, not 1. Comparing against 0 is safer than comparing against 1.
5. What's the difference between % and fmod()?
% works on integers and returns an integer remainder. fmod() works on floating-point and returns a floating-point remainder. For integers, 7 % 3 = 1. For doubles, fmod(7.5, 3.0) = 1.5. If you're working with integers, use %; if with floating-point, use fmod from <math.h> and link with -lm on Unix systems.
💡 Tip: once this works, try these classic % applications: (a) extract digits from a number (n % 10 gives the last digit), (b) check even/odd (n % 2), (c) convert seconds to hours/minutes/seconds using repeated / and %, (d) implement a circular buffer index that wraps (i % capacity). Each is a small extension of the same operator.