Recursive Digit Sum
Completed
Write a recursive digit_sum(n) for a non-negative integer.
Input
One integer.
Output
The sum of its digits.
Example
Input
12345
Output
15
Editor
Python
Hints
Last digit + digit_sum of the rest.
Solution explanation
Peeling off one digit per call shrinks the problem each time.
Show solution
def digit_sum(n):
if n < 10:
return n
return n % 10 + digit_sum(n // 10)
print(digit_sum(int(input())))