Coding basics · 12 of 20
Sum of digits
Return the sum of the digits of a non-negative integer n.
What you need
n % 10 is the last digit and n // 10 is everything before it. Together they take a number apart from the right.
n = 472 n % 10 # 2 n // 10 # 47
Examples
Input n = 1234
Output 10
1 + 2 + 3 + 4
Input n = 0
Output 0