All problems
Sum of Digits
Problem
Return the sum of the decimal digits of a non-negative integer n.
Peel off digits with n % 10 and divide by 10 until the number becomes 0. Return 0 for n = 0.
Hint
Repeated modulo and integer division visits every digit once.
Test cases
case 1
in: [0]
expected: 0
case 2
in: [123]
expected: 6
case 3
in: [9999]
expected: 36
case 4
in: [10]
expected: 1
case 5
in: [1000000]
expected: 1
Starter codePython
def sum_of_digits(n):
# Your code here
pass5 test cases · run your code against them in the editor