Sum of Digits in a Given Number
easySave
MathNumber Theory
Given a non-negative integer, return the sum of its digits.
Example 1
Input
[ 123 ]
Output
6
Explanation. The sum of digits in 123 is 1 + 2 + 3 = 6.
Example 2
Input
[ 10000 ]
Output
1
Explanation. The sum of digits in 10000 is 1 + 0 + 0 + 0 + 0 = 1.
Example 3
Input
[ 564 ]
Output
15
Explanation. The sum of digits in 564 is 5 + 6 + 4 = 15.
Example 4
Input
[ 99999 ]
Output
45
Explanation. The sum of digits in 99999 is 9 + 9 + 9 + 9 + 9 = 45.
Follow-up: Can you implement this solution recursively?
Constraints:
The input number will be a non-negative integer, not exceeding `99999`.
- Views
- 2