Math Fundamentals18 sections · 814 units
Open in Course

Extracting All Digits

Repeated modulo and division

You can extract all digits by repeating the process: take nmod10n \bmod 10 for the last digit, then set n:=n/10n := \lfloor n / 10 \rfloor.

For n=12345n = 12345: extract 55, nn becomes 12341234. Extract 44, nn becomes 123123. Extract 33, nn becomes 1212. Extract 22, nn becomes 11. Extract 11, nn becomes 00. Stop.

This pattern (modulo to extract, division to remove) appears in digit manipulation problems constantly.