CJCoding With Joseph
15free / daySign inCreate account
Back to Code Walkthrough

Sum of Digits Recursion

C++ code walkthrough · Question 33
hardRecursion

Code

Read carefully. What does this print?
1#include <iostream>
2using namespace std;
3int sumDigits(int n) {
4 if (n == 0) return 0;
5 return n % 10 + sumDigits(n / 10);
6}
7int main() {
8 cout << sumDigits(1234) << endl;
9 return 0;
10}

Your answer