Digit extraction in AP Computer Science A

Digit extraction is a standard AP Computer Science A algorithm (EK 2.9.A.1) that isolates each digit of an integer by repeatedly taking num % 10 to grab the rightmost digit and num /= 10 to chop it off, looping while num > 0.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is digit extraction?

Digit extraction is the standard algorithm for breaking an integer apart into its individual digits using only arithmetic. The pattern is always the same. Inside a while (num > 0) loop, num % 10 gives you the rightmost digit (because modulo by 10 returns the remainder, which is whatever's in the ones place), and num /= 10 chops that digit off (because integer division by 10 drops the ones place). So 834712 % 10 is 2, and 834712 / 10 is 83471. Repeat until num hits 0 and you've visited every digit.

One detail that trips people up. The digits come out right to left. For 834712, you process 2, then 1, then 7, and so on. For sums and counts that order doesn't matter, but if a question asks about digit position or building a reversed number, it absolutely does. The College Board lists identifying the individual digits in an integer as one of the named standard algorithms in EK 2.9.A.1, so this exact loop shape is fair game on the exam.

Why digit extraction matters in AP® Computer Science A

Digit extraction lives in Topic 2.9 (Implementing Selection and Iteration Algorithms) in Unit 2 and directly supports learning objective AP Comp Sci A 2.9.A, which says you develop code for standard algorithms and determine what they produce. EK 2.9.A.1 explicitly names "identify the individual digits in an integer" as one of the standard algorithms, right alongside divisibility checks, frequency counts, min/max, and sums/averages. That's the real payoff. Digit extraction almost never appears alone on the exam. It gets combined with those other standard algorithms, like summing the even digits or finding the smallest digit, so mastering this one loop unlocks a whole family of questions. It's also your best practice ground for the two operators AP CSA loves to test, modulo (%) and integer division (/).

How digit extraction connects across the course

Accumulator pattern (Unit 2)

Digit extraction is the supplier and the accumulator is the collector. The loop hands you one digit at a time, and a running variable like sum += digit or count++ gathers the result. Nearly every digit-extraction exam question is really these two patterns stacked together.

Minimum/maximum determination (Unit 2)

Swap the accumulator for a comparison and you get questions like "find the smallest digit." Start val at 9 (the biggest possible digit), extract each digit, and update val when you find something smaller. A Fiveable-style MCQ does exactly this with a findValue method.

Position accumulation (Unit 2)

Digit extraction's mirror image. Extraction tears a number down with % 10 and / 10; position accumulation builds one up with result = result * 10 + digit. Combine them and you can reverse an integer, a classic exam twist.

Is digit extraction on the AP® Computer Science A exam?

This shows up almost entirely as Unit 2 multiple choice, in two flavors. First, trace the code. You're given the loop and asked what prints. For example, summing only the even digits of 834712 (you'd extract 2, 1, 7, 4, 3, 8 and add 2 + 4 + 8 = 14). Second, fill in the missing code. A method like countOccurrences or sumEvenDigits has a blank where the num % 10 and num /= 10 lines belong, and you pick the segment that extracts correctly without skipping or repeating digits. Watch for the classic traps. Forgetting num /= 10 creates an infinite loop, swapping % and / breaks the logic, and assuming digits come out left to right gets you the wrong answer on order-sensitive questions. No released FRQ has centered on digit extraction by name, but the modulo and integer-division skills it drills appear constantly in FRQ method-writing.

Digit extraction vs num % 10 vs. num / 10

These two operations do opposite jobs inside the same loop, and mixing them up is the number one digit-extraction error. num % 10 reads the rightmost digit (it's the remainder, so 834712 % 10 = 2). num / 10 removes the rightmost digit (integer division truncates, so 834712 / 10 = 83471). Remember it as "mod looks, division throws away." You need both every iteration, or the loop either never sees digits or never ends.

Key things to remember about digit extraction

  • Digit extraction pulls apart an integer with a while loop, using num % 10 to get the rightmost digit and num /= 10 to remove it.

  • The loop condition is while (num > 0), and digits are processed from right to left, not left to right.

  • EK 2.9.A.1 names identifying the individual digits in an integer as a standard algorithm, so you're expected to both write and trace this loop.

  • Exam questions usually combine digit extraction with another standard algorithm, like an accumulator for digit sums or a comparison for the min or max digit.

  • Forgetting the num /= 10 line is the classic bug because num never changes and the loop runs forever.

  • Integer division in Java truncates, which is exactly why dividing by 10 cleanly drops the ones digit.

Frequently asked questions about digit extraction

What is digit extraction in AP Computer Science A?

It's the standard algorithm for isolating each digit of an integer using num % 10 to grab the rightmost digit and num /= 10 to remove it, inside a while (num > 0) loop. EK 2.9.A.1 lists it among the standard algorithms in Topic 2.9.

Does digit extraction process digits left to right?

No, it goes right to left. For 834712 you extract 2 first, then 1, then 7, and so on, because % 10 always reads the ones place. Order doesn't matter for sums or counts, but it matters a lot for position-based or number-reversal questions.

What's the difference between num % 10 and num / 10?

num % 10 gives the remainder, which is the rightmost digit (834712 % 10 = 2). num / 10 uses integer division to drop that digit (834712 / 10 = 83471). You need both each loop iteration, one to read and one to remove.

Why does my digit extraction loop run forever?

You almost certainly forgot num /= 10. Without it, num never shrinks, so the condition num > 0 stays true forever. Every digit-extraction loop needs both the % 10 read and the /= 10 chop.

How do I sum or count digits with digit extraction?

Pair it with an accumulator. Declare sum = 0 or count = 0 before the loop, then inside the loop test or add each extracted digit, like if (digit % 2 == 0) sum += digit. That exact combo appears in AP-style multiple choice.