Peeling digits off a number with recursion.
Two operators do all the work: % 10 isolates the last digit; / 10 drops it. Recursion handles the rest.
To process a number digit by digit recursively, two integer operations form the toolkit:
n % 10— the last digit ofn.n / 10—nwith the last digit removed (integer division).
Together they let the method "peel" one digit per call and recurse on the smaller number. Reaching 0 means no digits left — the base case.
On the AP exam, digit recursion appears in digit-sum, digit-count, and digit-reverse FRQs.
Process the last digit; recurse on what's left.
Base case at n == 0.
The template
public int digitMethod(int n) {
if (n == 0) return 0; // or 1 for counts
int last = n % 10;
return last_processed + digitMethod(n / 10);
}
Sum digits
public int sumDigits(int n) {
if (n == 0) return 0;
return (n % 10) + sumDigits(n / 10);
}
Adds the last digit to the sum of the rest.
Count digits
public int countDigits(int n) {
if (n == 0) return 0;
return 1 + countDigits(n / 10);
}
Each call contributes 1 (one digit); base case contributes 0. For n = 1234, returns 4.
Why integer division works here
1234 / 10 = 123 — Java truncates the decimal portion. That's exactly what we want: drop the last digit. The remainder we already captured with % 10.
Edge case: n = 0
For sumDigits(0), the spec usually says "the sum of digits of 0 is 0". The base case handles this. For countDigits(0), opinions differ — sometimes 0, sometimes 1 — so AP problems usually test only n > 0.
Digit-recursion vocabulary.
Used in every digit-processing FRQ.
- Last digit —
n % 10. - Truncated quotient —
n / 10. - Integer division — drops the decimal portion.
- Peel — isolating one digit per call.
- Digit count — number of digits in n.
- Digit sum — total of all digits.
How digit recursion is tested.
Base case and operator choice dominate.
- Base case at n == 0. Returns 0 (sum) or 0 (count).
%isolates;/drops. Two operators, two purposes.- Integer division required. Both operands must be int.
- Add 1 for count; add the digit for sum. Different recursive contribution.
MCQ patterns: trace sumDigits(123) call by call; verify base case stops correctly.
Trace sumDigits(345) and countDigits(345).
Same call structure, different return contribution.
sumDigits(345) — calls down:
- sumDigits(345) = 5 + sumDigits(34)
- sumDigits(34) = 4 + sumDigits(3)
- sumDigits(3) = 3 + sumDigits(0)
- sumDigits(0) returns 0 (base case)
Returns up: sumDigits(3) = 3; sumDigits(34) = 7; sumDigits(345) = 12.
countDigits(345) — calls down:
- countDigits(345) = 1 + countDigits(34)
- countDigits(34) = 1 + countDigits(3)
- countDigits(3) = 1 + countDigits(0)
- countDigits(0) returns 0
Returns up: countDigits(3) = 1; countDigits(34) = 2; countDigits(345) = 3.
Why this matters. Same structure — peel last digit, recurse on rest — but the recursive case contributes the digit (for sum) or 1 (for count).
Digit-recursion pitfalls.
Most swap the two operators or miss the base case.
- Using
n / 10for the digit. That's the quotient, not the digit. - Using
n % 10for the recursion. That recurses on a digit, not on the shrunken number. - Base case at
n < 10. Works but skips the natural stop at 0. - No base case. n / 10 of 0 is 0; recursion never terminates.
- Double-counting the last digit. Don't sum it twice in the recursive case.
Digit operations.
Two operators, two purposes.
Digit Recursion Reference
AP Quick Reference| Operation | Code | Meaning |
|---|---|---|
| Last digit | n % 10 | Remainder mod 10. |
| Drop last digit | n / 10 | Integer quotient. |
| Base case | n == 0 | No digits left. |
| Sum contribution | n % 10 + ... | Add digit. |
| Count contribution | 1 + ... | Add 1 per call. |
How to master digit recursion.
% for digit; / for recursion.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each option, verify
% 10is used for the digit and/ 10for the recursive argument. - Java Lab — write sumDigits, countDigits, and a digit-product method. Trace on 3-digit inputs.
- FRQ Practice — base case at 0; recursive call on n / 10; digit operation on n % 10.
Memorize the pair: % for "this digit", / for "everything else". Then digit recursion writes itself.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 11 Topic 04 Numeric Digit Recursion FRQ Practice
AP-style topic practice assessment