CJCoding With Joseph

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:

10

(1 + 2 + 3 + 4 = 10.) The base case is n == 0 returning 0; otherwise return n % 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.