← Back to Functions
Question 411
Recursive Sum of Digits
Recursive Sum of Digits
Write a recursive functionint sumDigits(int n)that returns the sum of the digits of a non-negative integern. Call it withsumDigits(1234)and print the result (see Expected Output). (1 + 2 + 3 + 4 = 10.) The base case isn == 0returning0; otherwise returnn % 10 + sumDigits(n / 10).
Expected Output:
10= newline. The output ends with a newline — 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.