01 / 20
%

Modulus &
Remainders

Master the modulus operator \( % \) in C++ to find remainders, detect patterns, and solve problems with integer arithmetic.

C++

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.

\( 17 \mathbin{\%} 5 \)
Result: 2
Because \( 17 = 3 \times 5 + 2 \)
\( 20 \mathbin{\%} 4 \)
Result: 0
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 \).

= x +
\(a\)
\(q\)
\(b\)
\(r\)
\(a \mathbin{/} b\)
\(a \mathbin{\%} b\)
dividend quotient divisor remainder

Why Modulus Matters

The \( % \) operator appears in countless programming patterns. Understanding remainders unlocks elegant solutions to many problems.

Even/Odd Check
\( n \mathbin{\%} 2 == 0 \) tells you if a number is even
Digit Extraction
\( n \mathbin{\%} 10 \) gives the last digit of \( n \)
Cyclic Patterns
\( i \mathbin{\%} k \) wraps indices around in arrays and loops

The Core Rule of \( % \)

0 5 10 15 17 +5 +5 +5 remainder = 2
\( 17 \mathbin{\%} 5 = 17 - (3 \times 5) = 2 \)

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.

\( 7 \mathbin{\%} 3 \)
Result: 1
Positive dividend, positive result
\( -7 \mathbin{\%} 3 \)
Result: -1
Negative dividend, negative result
\( 7 \mathbin{\%} (-3) \)
Result: 1
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.

Possible results of \( n \mathbin{\%} 5 \)
0 1 2 3 4 For non-negative n: exactly 5 possible values

How to Apply Modulus

1
Identify the dividend and divisor. The dividend goes left of \( % \), the divisor goes right.
2
Perform integer division. Compute \( a / b \) with truncation toward zero to get the quotient \( q \).
3
Multiply back. Compute \( q \times b \) to see how much the divisor accounts for.
4
Subtract to find the remainder. The remainder is \( r = a - q \times b \).
5
Check the sign. In C++, \( r \) has the same sign as \( a \). Verify your answer.

Example 1: Even or Odd?

The simplest use of modulus: checking if a number is even or odd by dividing by 2.

int main() { int n = 42; if (n % 2 == 0) { cout << "Even"; // Prints "Even" } else { cout << "Odd"; } }

Example 2: Extract Last Digit

Using \( n \mathbin{\%} 10 \) gives the ones digit. This is key for digit-processing problems.

int n = 7349; int last = n % 10; // last = 9 int tens = (n / 10) % 10; // tens = 4
7 3 4 9
\( n \mathbin{\%} 10 \)
\( (n/10) \mathbin{\%} 10 \)

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.

// Cycle through 4 colors: 0, 1, 2, 3, 0, 1, 2, ... for (int i = 0; i < 10; i++) { int color = i % 4; cout << color << " "; } // Output: 0 1 2 3 0 1 2 3 0 1
0 1 2 3
\( i=0: 0\%4=0 \)
\( i=1: 1\%4=1 \)
\( i=2: 2\%4=2 \)
\( i=3: 3\%4=3 \)
\( i=4: 4\%4=0 \)

Example 4: Sum of Digits

Combine \( % \) and \( / \) in a loop to extract every digit of a number and accumulate their sum.

int n = 5274, sum = 0; while (n > 0) { sum += n % 10; // add last digit n /= 10; // remove last digit } // sum = 4 + 7 + 2 + 5 = 18

Common Mistakes

✕ Using % with Floats
5.5 % 2 is a compile error in C++. The \( % \) operator only works with integer types. Use fmod() for floats.
✕ Division by Zero
n % 0 is undefined behavior. Always check that the divisor is non-zero before using modulus.
✕ Ignoring Sign Rules
Assuming \( -7 \mathbin{\%} 3 = 2 \). In C++, the result is -1 because the remainder follows the sign of the dividend.

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;

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?