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 the remaining digits recursively.Examples
How to Think About It
n % 10 and add it to the sum of digits of the remaining number found by n / 10. Repeat this until the number becomes zero, which is the base case.Algorithm
Code
#include <stdio.h> int sumDigits(int n) { if (n == 0) return 0; return (n % 10) + sumDigits(n / 10); } int main() { int num = 1234; printf("Sum of digits of %d is %d\n", num, sumDigits(num)); return 0; }
Dry Run
Let's trace the input 1234 through the recursive sumDigits function.
Initial call
sumDigits(1234) calculates 1234 % 10 = 4 and calls sumDigits(1234 / 10 = 123)
Second call
sumDigits(123) calculates 123 % 10 = 3 and calls sumDigits(12)
Third call
sumDigits(12) calculates 12 % 10 = 2 and calls sumDigits(1)
Fourth call
sumDigits(1) calculates 1 % 10 = 1 and calls sumDigits(0)
Base case
sumDigits(0) returns 0 because number is zero
Returning results
sumDigits(1) returns 1 + 0 = 1 sumDigits(12) returns 2 + 1 = 3 sumDigits(123) returns 3 + 3 = 6 sumDigits(1234) returns 4 + 6 = 10
| Call | Number | Last Digit (n % 10) | Remaining Number (n / 10) | Return Value |
|---|---|---|---|---|
| 1 | 1234 | 4 | 123 | 10 |
| 2 | 123 | 3 | 12 | 6 |
| 3 | 12 | 2 | 1 | 3 |
| 4 | 1 | 1 | 0 | 1 |
| 5 | 0 | 0 | 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 extracts 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 at each step.
Alternative Approaches
#include <stdio.h> int sumDigitsIter(int n) { int sum = 0; while (n > 0) { sum += n % 10; n /= 10; } return sum; } int main() { int num = 1234; printf("Sum of digits of %d is %d\n", num, sumDigitsIter(num)); return 0; }
#include <stdio.h> #include <string.h> int sumDigitsStr(const char *str) { if (*str == '\0') return 0; return (*str - '0') + sumDigitsStr(str + 1); } int main() { char numStr[] = "1234"; printf("Sum of digits of %s is %d\n", numStr, sumDigitsStr(numStr)); return 0; }
Complexity: O(d) time, O(d) space
Time Complexity
The function processes each digit once, so time grows linearly with the number of digits d.
Space Complexity
Each recursive call adds a layer to the call stack, so space used is proportional to the number of digits.
Which Approach is Fastest?
The iterative approach uses constant 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(d) | O(d) | Learning recursion and simple code |
| Iterative | O(d) | O(1) | Performance and memory efficiency |
| String Conversion | O(d) | O(d) | When input is a string or for character processing |