01
Overview

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 of n.
  • n / 10n with 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.

02
Core Concept

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.

03
Key Vocabulary

Digit-recursion vocabulary.

Used in every digit-processing FRQ.

Vocabulary
  • Last digitn % 10.
  • Truncated quotientn / 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.
04
AP Exam Focus

How digit recursion is tested.

Base case and operator choice dominate.

Exam Focus
  • 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.

05
Worked Example

Trace sumDigits(345) and countDigits(345).

Same call structure, different return contribution.

Worked Example AP-style reasoning

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).

06
Common Mistakes

Digit-recursion pitfalls.

Most swap the two operators or miss the base case.

Watch Out
  • Using n / 10 for the digit. That's the quotient, not the digit.
  • Using n % 10 for 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.
07
Reference Table

Digit operations.

Two operators, two purposes.

Digit Recursion Reference

AP Quick Reference
OperationCodeMeaning
Last digitn % 10Remainder mod 10.
Drop last digitn / 10Integer quotient.
Base casen == 0No digits left.
Sum contributionn % 10 + ...Add digit.
Count contribution1 + ...Add 1 per call.
08
Practice Tip

How to master digit recursion.

% for digit; / for recursion.

Practice Strategy

Use the three practice tools below this lesson in order:

  • MCQ Practice — for each option, verify % 10 is used for the digit and / 10 for 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.

09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 11 Topic 04 Numeric Digit Recursion FRQ Practice

AP-style topic practice assessment

Start