C++ Program for Sum of Digits Using Recursion
int sumDigits(int n) { if (n == 0) return 0; return n % 10 + sumDigits(n / 10); } which adds the last digit to the sum of digits of the remaining number.Examples
How to Think About It
n % 10, then call the same process on the rest of the number using n / 10. Keep doing this until the number becomes zero, then add all the digits together.Algorithm
Code
#include <iostream> using namespace std; int sumDigits(int n) { if (n == 0) return 0; return n % 10 + sumDigits(n / 10); } int main() { int number; cout << "Enter a number: "; cin >> number; cout << "Sum of digits: " << sumDigits(number) << endl; return 0; }
Dry Run
Let's trace the number 123 through the code to see how the sum of digits is calculated.
Initial call
sumDigits(123) -> last digit 3 + sumDigits(12)
Second call
sumDigits(12) -> last digit 2 + sumDigits(1)
Third call
sumDigits(1) -> last digit 1 + sumDigits(0)
Base case
sumDigits(0) -> returns 0
Return values
sumDigits(1) = 1 + 0 = 1 sumDigits(12) = 2 + 1 = 3 sumDigits(123) = 3 + 3 = 6
| Function Call | Last Digit | Remaining Number | Partial Sum |
|---|---|---|---|
| sumDigits(123) | 3 | 12 | 3 + sumDigits(12) |
| sumDigits(12) | 2 | 1 | 2 + sumDigits(1) |
| sumDigits(1) | 1 | 0 | 1 + sumDigits(0) |
| sumDigits(0) | - | - | 0 |
Why This Works
Step 1: Base Case
The function stops calling itself when the number becomes zero, returning 0 to end recursion.
Step 2: Extract Last Digit
Using n % 10, the function gets the last digit of the number to add to the sum.
Step 3: Recursive Call
The function calls itself with the number divided by 10, removing the last digit, to process the remaining digits.
Step 4: Sum Accumulation
Each recursive call returns the sum of digits processed so far, adding the last digit until the full sum is returned.
Alternative Approaches
#include <iostream> using namespace std; int sumDigitsIterative(int n) { int sum = 0; while (n > 0) { sum += n % 10; n /= 10; } return sum; } int main() { int number; cout << "Enter a number: "; cin >> number; cout << "Sum of digits: " << sumDigitsIterative(number) << endl; return 0; }
#include <iostream> #include <string> using namespace std; int sumDigitsString(int n) { string s = to_string(n); int sum = 0; for (char c : s) { sum += c - '0'; } return sum; } int main() { int number; cout << "Enter a number: "; cin >> number; cout << "Sum of digits: " << sumDigitsString(number) << endl; return 0; }
Complexity: O(log n) time, O(log n) space
Time Complexity
The function processes each digit once, and the number of digits is proportional to log base 10 of n, so time is O(log n).
Space Complexity
Each recursive call adds a layer to the call stack, so space used is proportional to the number of digits, O(log n).
Which Approach is Fastest?
The iterative approach uses O(1) space and is generally faster due to no function call overhead, but recursion is elegant and easy to understand.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursive | O(log n) | O(log n) | Learning recursion, simple code |
| Iterative | O(log n) | O(1) | Performance and memory efficiency |
| String Conversion | O(log n) | O(log n) | Simple code, but uses extra memory |