Recursive Sum of Digits
mediumQuestion 411Write 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, so print one after the last line.
Hints3 available
Sign in to use hintsTopics
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.