Modulus &
Remainders
Master the modulus operator \( % \) in C++ to find remainders, detect patterns, and solve problems with integer arithmetic.
What is the Modulus Operator?
The modulus operator \( % \) returns the remainder after integer division. When you write \( a \mathbin{\%} b \), C++ divides \( a \) by \( b \) and gives you what's left over.
Because \( 17 = 3 \times 5 + 2 \)
Because \( 20 = 5 \times 4 + 0 \)
The Division Relationship
Every integer division can be expressed with this fundamental identity. The modulus operator gives us the remainder \( r \).
Why Modulus Matters
The \( % \) operator appears in countless programming patterns. Understanding remainders unlocks elegant solutions to many problems.
The Core Rule of \( % \)
Modulus with Negative Numbers
In C++, the result of \( % \) has the same sign as the dividend (left operand). This is different from the mathematical modulo definition.
Positive dividend, positive result
Negative dividend, negative result
Positive dividend, positive result
\( % \) vs \( / \): Key Distinction
Both use the same division, but they give different pieces of the result. Understanding this distinction is essential.
| Integer Division \( / \) | Modulus \( % \) | |
|---|---|---|
| Returns | Quotient (how many times) | Remainder (what's left) |
| \( 17, 5 \) | 3 | 2 |
| \( 25, 7 \) | 3 | 4 |
| \( 100, 10 \) | 10 | 0 |
| Operand types | int, float, double | int only |
The Range of Results
The result of \( a \mathbin{\%} b \) is always constrained. Knowing the possible range helps you predict output.
How to Apply Modulus
Example 1: Even or Odd?
The simplest use of modulus: checking if a number is even or odd by dividing by 2.
Example 2: Extract Last Digit
Using \( n \mathbin{\%} 10 \) gives the ones digit. This is key for digit-processing problems.
Example 3: Circular Wrapping
Modulus creates circular patterns: after reaching the maximum, values wrap back to zero. This is essential for arrays, clocks, and cycles.
Example 4: Sum of Digits
Combine \( % \) and \( / \) in a loop to extract every digit of a number and accumulate their sum.
Common Mistakes
5.5 % 2 is a compile error in C++. The \( % \) operator only works with integer types. Use fmod() for floats.n % 0 is undefined behavior. Always check that the divisor is non-zero before using modulus.Modulus Cheat Sheet
| Pattern | Expression | Purpose |
|---|---|---|
| Even/Odd | n % 2 |
0 = even, non-zero = odd |
| Last digit | n % 10 |
Extract ones place |
| Divisibility | a % b == 0 |
Check if b divides a |
| Wrap around | i % size |
Circular buffer/index |
| Every k-th | i % k == 0 |
Do something every k iterations |
| Clock math | (h + n) % 12 |
Hours after adding n |
Question 1 of 5
What is the value of \( 23 \mathbin{\%} 7 \)?
Question 2 of 5
What does \( -10 \mathbin{\%} 3 \) evaluate to in C++?
Question 3 of 5
Which expression correctly checks if an integer n is divisible by 6?
Question 4 of 5
What will the following code output?
int x = 157;
int d = 0;
while (x > 0) {
d += x % 10;
x /= 10;
}
cout << d;
% 10 and removes it with /= 10.Question 5 of 5
In a game, a player's position wraps around a board of 8 squares (indexed 0 to 7). If the player is at square 5 and moves forward 11 spaces, what is their new position?