CJCoding With Joseph
← Back to Question List
C++ Code Walkthrough #33

Sum of Digits Recursion

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