Objective
A sum of digits has a natural recursive definition: the sum of a number is its last digit plus the sum of the rest of the number.
Instructions
- Define
sum_digits(n)recursively: ifnhas a single digit (n < 10) returnn; otherwise returnn % 10 + sum_digits(n // 10). - Read
kand thenknon-negative integers, one per line. - For each one show
n: sum. Do not use loops or convert the number into text.
3
123
9
9875
123: 6
9: 9
9875: 29
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise