Exit
  1. Learn
  2. Check
  3. Solve
  4. Reflect

Peel the last digit off

Think of a number as a stack of digit cards. Divide 4729 by 10 and the remainder is 9, which is exactly the last digit. Whole-number division by 10 gives 472, which is the same number with that last digit torn off.

Two small moves give you everything: read the last digit, then drop it.

Remainder after dividing by 10: is the last digit.
Whole-number division by 10: drops the last digit.
In code
4729 % 10    # 9
4729 // 10   # 472
Digits of 4729, right to left
4
0
7
1
2
2
9
3
digit

4729 % 10 = 9. total = 9, n becomes 472.

Move 1 of 4

Repeat until nothing is left

Do it again and again. Read the last digit, add it to a running total, then drop it. When the number reaches 0 there are no digits left, so you have met every one of them, from right to left.

What if the number is 0 to begin with? The loop never runs and the total stays 0, which is the right answer for a sum.

In Java and C++: dividing two whole numbers already rounds down for positive values.
In code
total = 0
while n > 0:
    total += n % 10
    n //= 10
return total