← Recursion

Recursive Digit Sum

medium

Compute the sum of n’s decimal digits recursively – the same problem as Module 1’s iterative “Digit Sum” exercise, solved with a different technique.

The base case is a single-digit number (n < 10), which is its own digit sum. Otherwise, split n into its last digit (n % 10) and everything before it (n / 10, exactly as before), and let a recursive call handle “everything before it” while you add the last digit on. Notice the base case here isn’t “n reaches exactly 0” the way the while version’s loop condition was – it’s “n is already a single digit,” a subtly different (and for this problem, more natural) stopping point.

Your solution

"Run" uses the sample stdin ("493"). "Submit" checks your code against all 4 test cases.

Test results

Submit your solution to run it against all test cases.

Hints

    Reference solution