CJCoding With Joseph
15free / daySign inCreate account

Digit Sum Until One Digit

Write two recursive functions. digit_sum(n: int) -> int returns the sum of the digits of a non-negative integer: a number below 10 is its own sum, otherwise it is the last digit (the remainder operator % with 10) plus the digit sum of n // 10. digital_root(n: int) -> int keeps replacing the number with the sum of its digits until a single digit is left: a number below 10 is returned as it is; otherwise compute digit_sum(n), store it in total, print total on its own line, and return the digital root of total. So the function prints every sum it computes, including the final single digit, and returns that digit; the tests then print the returned value on one last line in the form Root: 2. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one integer n, no prompt, which the tests read and pass to digital_root before printing the labelled value it returns.

Expected Output:

29
11
2
Root: 2

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
RecursionOperators
📥 Auto-Input:
9875\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.