CJCoding With Joseph
15free / daySign inCreate account

Recursive Sum of Digits

Write a recursive function int sumDigits(int n) that returns the sum of the digits of a non-negative integer n. Call it with sumDigits(1234) and print the result (see Expected Output).

(1 + 2 + 3 + 4 = 10.) The base case is n == 0 returning 0; otherwise return n % 10 + sumDigits(n / 10).

Expected Output:

10

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

Hints3 available
Topics:
Functions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.