Sum of Digits
Easy
20
1
79.6% Acceptance
In this lab you need to implement a function named sum_of_digits
in Python. This function takes a single integer parameter and calculates the sum of its digits. The process repeats on the sum until a single-digit number is obtained. The function should then return this single-digit result.
For example:
- If the input is
29
, the function calculates2 + 9 = 11
, then repeats the process to get1 + 1 = 2
. Thus, the output is2
. - For
12345
, the sum is1 + 2 + 3 + 4 + 5 = 15
, then1 + 5 = 6
. Therefore, the output is6
.
The function should handle inputs ranging from single-digit numbers to multi-digit numbers, including edge cases like 0
. Your implementation will be tested against a series of challenges to validate its correctness under various scenarios.