โ 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: 10 (1 + 2 + 3 + 4 = 10.) The base case isn == 0returning0; otherwise returnn % 10 + sumDigits(n / 10).
Expected Output:
10
Topics:
Functions
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท 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.