206. Sum of Digits by Recursion
Foundation1000 ms256 MBSolved by 0%
Print the sum of the digits of n.
123 -> 1 + 2 + 3 = 6
Two operations do all the work. n % 10 gives the last digit and
n / 10 removes it, so the sum is the last digit plus the digit sum of what
is left.
Constraints - `0 ≤ n ≤ 1000000000`
Input
A single line containing one integer n.
Output
Print one integer, the sum of the digits of n.
Input123
Output6
Noteis the worked example
Input0
Output0
Notehas a digit sum of 0, unlike the digit COUNT which is 1
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.