Goal: Walk through a number's digits with % 10 and // 10.
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.
4729 % 10 # 9 4729 // 10 # 472
Digits of 8165, right to left
8
01
16
25
3digit
8165 % 10 = 5. total = 5, n becomes 816.
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.
count = 0
while n > 0:
n //= 10
count += 1