Digital Root - Problem
Write a function that takes a positive integer and repeatedly sums its digits until only a single digit remains. This single digit is called the digital root.
For example:
16→1 + 6 = 7(single digit, so return 7)38→3 + 8 = 11→1 + 1 = 2(return 2)942→9 + 4 + 2 = 15→1 + 5 = 6(return 6)
Requirements: Implement both iterative and recursive solutions.
Input & Output
Example 1 — Basic Case
$
Input:
n = 942
›
Output:
6
💡 Note:
Step-by-step: 942 → 9 + 4 + 2 = 15 → 1 + 5 = 6. Since 6 is a single digit, return 6.
Example 2 — Single Digit Input
$
Input:
n = 7
›
Output:
7
💡 Note:
Since 7 is already a single digit, return it directly without any processing.
Example 3 — Multiple Iterations
$
Input:
n = 9999
›
Output:
9
💡 Note:
9999 → 9 + 9 + 9 + 9 = 36 → 3 + 6 = 9. The digital root is 9.
Constraints
- 1 ≤ n ≤ 109
- n is a positive integer
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code