Integer division and the modulo operator.
Two operators that split a number into quotient and remainder — the foundation of digit extraction, time conversion, and divisibility tests.
In Java, when both operands of / are integers, the result is the integer quotient — the decimal part is truncated. The % operator returns the remainder of that division.
Together they decompose a number: n = (n / k) * k + (n % k). This identity drives algorithms for digit extraction (last digit, count digits), time formatting, and place-value problems.
On the AP exam, these operators appear in MCQs about expression evaluation and in FRQs involving digit manipulation.
Truncating division; non-negative remainder for positives.
Both operands integers means integer result.
Basics
7 / 2 // 3 (truncated, not rounded)
7 % 2 // 1 (remainder)
10 / 3 // 3
10 % 3 // 1
For positive operands, % returns a value in [0, k).
Type matters
7 / 2 // 3 (int / int)
7.0 / 2 // 3.5 (double promotes the other operand)
(double) 7 / 2 // 3.5
If you need a decimal result, cast at least one operand to double.
Digit extraction
int n = 1234;
int lastDigit = n % 10; // 4
int withoutLastDigit = n / 10; // 123
Repeatedly applying both peels off digits right to left.
Time conversion
int totalSeconds = 3725;
int minutes = totalSeconds / 60; // 62
int seconds = totalSeconds % 60; // 5
int hours = minutes / 60; // 1
int remainingMinutes = minutes % 60; // 2
Division and modulo vocabulary.
Used in every arithmetic FRQ.
- Integer division —
/on two ints, truncates decimal. - Modulo —
%, remainder after division. - Truncation — discarding the fractional part.
- Quotient — result of division.
- Remainder — result of modulo.
- Cast — converting types to control operation behavior.
How / and % are tested.
Truncation, casting, and digit-extraction dominate.
- Truncation, not rounding.
7 / 2is 3, never 4. - Cast for decimals.
(double) sum / countavoids integer truncation. - Negative modulo.
-7 % 3is-1— sign follows dividend. - Division by zero.
n / 0on ints throwsArithmeticException.
Sum the digits of a number.
Classic application of both operators.
Problem. Return the sum of the digits of n.
public int digitSum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n = n / 10;
}
return sum;
}
Trace. For n = 1234:
- 1234 % 10 = 4; sum = 4; n = 123.
- 123 % 10 = 3; sum = 7; n = 12.
- 12 % 10 = 2; sum = 9; n = 1.
- 1 % 10 = 1; sum = 10; n = 0.
- Loop exits.
Answer: 10.
Division and modulo pitfalls.
Mostly type and rounding mistakes.
- Forgetting the cast. Integer division truncates without warning.
- Assuming rounding. Java truncates toward zero, not rounds.
- Negative modulo surprises.
-7 % 3is-1. - Division by zero on ints. Exception at runtime.
/ and % behavior.
Quick lookup.
Division Reference
AP Quick Reference| Expression | Result | Note |
|---|---|---|
7 / 2 | 3 | Truncated. |
7 % 2 | 1 | Remainder. |
7.0 / 2 | 3.5 | Double division. |
-7 % 3 | -1 | Sign follows dividend. |
n / 0 (int) | Exception | ArithmeticException. |
How to master / and %.
Drill digit-extraction; cast for decimals.
- MCQ Practice — for each expression, identify the operand types first.
- Java Lab — implement digitSum, countDigits, reverseNumber. Test negatives.
- FRQ Practice — cast before dividing for percentages.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 09 Topic 03 Integer Division and Modulo FRQ Practice
AP-style topic practice assessment