01 / 21
%

Modulus &
Remainders

Master the % operator in Java: understand how division produces remainders, and learn to harness them for even/odd checks, cyclic patterns, and digit extraction.

Java

What Is the Modulus Operator?

In Java, the % operator (called "modulus" or "mod") returns the remainder after integer division. If you divide \( a \) by \( b \), the quotient is \( a / b \) and the leftover is \( a \% b \).

Think of it like sharing cookies: if you have 17 cookies and 5 friends, each friend gets \( 17 / 5 = 3 \) cookies, and you have \( 17 \% 5 = 2 \) cookies left over.

Visualizing \( 17 \% 5 \)

0 5 5 5 10 5 15 2 17 Quotient: 3 groups of 5 Remainder: 2
\( 17 \% 5 = 2 \)

Why Modulus Matters in Java

Even/Odd
\( n \% 2 == 0 \) tells you instantly whether a number is even.
Cyclic Patterns
Wrap array indices, clock arithmetic, and repeating schedules.
Digit Extraction
\( n \% 10 \) extracts the last digit of any integer.

The Core Rule of \( \% \)

\( a \% b = a - (a / b) \times b \)
dividend divisor integer quotient (truncated) Java truncates toward zero (no rounding)

Sign Rules: Negative Numbers

In Java, the sign of the result of \( a \% b \) always follows the sign of the dividend (\( a \)), regardless of the sign of \( b \).

Expression \( a / b \) \( a \% b \)
\( 7 \% 3 \) 2 1
\( -7 \% 3 \) -2 -1
\( 7 \% (-3) \) -2 1
\( -7 \% (-3) \) 2 -1

\( \% \) vs \( / \): Key Distinction

/ Integer Division
Returns how many times \( b \) fits into \( a \). Discards the fractional part (truncates toward zero).
% Modulus
Returns what's left over after dividing. Always satisfies: \( a == (a/b)*b + a\%b \).
int a = 17, b = 5; System.out.println(a / b); // 3 (quotient) System.out.println(a % b); // 2 (remainder)

How to Apply \( \% \) Step by Step

1
Identify the dividend and divisor. The expression \( a \% b \) means: \( a \) is divided by \( b \).
2
Compute the integer quotient. Use \( a / b \) (truncated toward zero).
3
Multiply back. Compute \( (a / b) \times b \).
4
Subtract to find the remainder. \( a \% b = a - (a / b) \times b \).
5
Check the sign. The remainder takes the sign of \( a \) (the dividend).

Example 1: Even or Odd?

Write a method that returns true if a number is even.

public static boolean isEven(int n) { return n % 2 == 0; }
\( 14 \% 2 \)
= 0 → Even
\( 9 \% 2 \)
= 1 → Odd
\( -3 \% 2 \)
= -1 → Odd

Example 2: Extract the Last Digit

Using \( n \% 10 \) pulls out the ones place of any integer.

int n = 3927; int lastDigit = n % 10; // 7
3 9 2 7
\( n \% 10 = 7 \)
\( n / 10 = 392 \)

Example 3: Cyclic Wrapping

When an index reaches the end of an array, mod wraps it back to the start. This is the basis of circular buffers and round-robin scheduling.

String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri"}; for (int i = 0; i < 12; i++) { System.out.print(days[i % 5] + " "); } // Mon Tue Wed Thu Fri Mon Tue Wed Thu Fri Mon Tue
0 1 2 3 4
\( 5 \% 5 = 0 \)
\( 7 \% 5 = 2 \)
\( 11 \% 5 = 1 \)
Always wraps to 0..4

Real-World: 24-Hour Time

A 24-hour clock is a perfect example of modular arithmetic. If it's 22:00 (10 PM), what time will it be in 5 hours?

int currentHour = 22; int hoursLater = 5; int newHour = (currentHour + hoursLater) % 24; // (22 + 5) % 24 = 27 % 24 = 3
\( 27 \% 24 \)
= 3 → 03:00
\( 50 \% 24 \)
= 2 → 02:00

Classic Pattern: FizzBuzz

The most famous coding interview question combines two modulus checks:

for (int i = 1; i <= 100; i++) { if (i % 15 == 0) System.out.println("FizzBuzz"); else if (i % 3 == 0) System.out.println("Fizz"); else if (i % 5 == 0) System.out.println("Buzz"); else System.out.println(i); }

Example 4: Sum of Digits

Use \( \% 10 \) and \( / 10 \) in a loop to peel off digits one at a time:

public static int digitSum(int n) { int sum = 0; n = Math.abs(n); // handle negatives while (n > 0) { sum += n % 10; // grab last digit n = n / 10; // remove last digit } return sum; }
1
\( 3927 \% 10 = \) 7, then \( 3927 / 10 = 392 \)
2
\( 392 \% 10 = \) 2, then \( 392 / 10 = 39 \)
3
\( 39 \% 10 = \) 9, then \( 39 / 10 = 3 \)
4
\( 3 \% 10 = \) 3, then \( 3 / 10 = 0 \) → stop. Sum = 21

Common Mistakes

✕ Division by Zero
Using n % 0 throws an ArithmeticException at runtime. Always guard against zero divisors.
✕ Odd Check with == 1
Writing n % 2 == 1 fails for negative numbers since \( -3 \% 2 = -1 \). Use n % 2 != 0 instead.
✕ Mixing % with Doubles
Java allows % on doubles, but floating-point errors make it unreliable. Stick to integers for modulus.

Modulus Cheat Sheet

Pattern Expression Result
Even check \( n \% 2 == 0 \) true/false
Last digit \( n \% 10 \) 0..9
Cyclic wrap \( i \% \text{length} \) 0..length-1
Divisibility \( n \% k == 0 \) true/false
Remove last digit \( n / 10 \) truncated int
24-hour time \( (h + d) \% 24 \) 0..23

Question 1 of 5

What is the value of \( 23 \% 7 \) in Java?

Question 2 of 5

What does \( -13 \% 5 \) evaluate to in Java?

Question 3 of 5

Which expression correctly checks if an integer n is odd, even when n is negative?

Question 4 of 5

Given int[] arr = {10, 20, 30};, what does arr[7 % 3] evaluate to?

Question 5 of 5

What is the output of the following code?

int x = 456; System.out.println(x % 10 + x / 100);