0
0
CProgramBeginner · 2 min read

C Program for Sum of Digits Using Recursion

You can find the sum of digits of a number in C using recursion by defining a function like 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

Input1234
Output10
Input0
Output0
Input9
Output9
🧠

How to Think About It

To find the sum of digits using recursion, think of the number as a collection of digits. Take the last digit using 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

1
Get the input number.
2
Check if the number is zero; if yes, return zero.
3
Find the last digit by taking the remainder of division by 10.
4
Call the function recursively with the number divided by 10 (removing the last digit).
5
Add the last digit to the result of the recursive call and return it.
💻

Code

c
#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;
}
Output
Sum of digits of 1234 is 10
🔍

Dry Run

Let's trace the input 1234 through the recursive sumDigits function.

1

Initial call

sumDigits(1234) calculates 1234 % 10 = 4 and calls sumDigits(1234 / 10 = 123)

2

Second call

sumDigits(123) calculates 123 % 10 = 3 and calls sumDigits(12)

3

Third call

sumDigits(12) calculates 12 % 10 = 2 and calls sumDigits(1)

4

Fourth call

sumDigits(1) calculates 1 % 10 = 1 and calls sumDigits(0)

5

Base case

sumDigits(0) returns 0 because number is zero

6

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

CallNumberLast Digit (n % 10)Remaining Number (n / 10)Return Value
11234412310
21233126
312213
41101
50000
💡

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

Iterative approach
c
#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;
}
This uses a loop instead of recursion, which can be easier to understand and avoids function call overhead.
Using string conversion
c
#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;
}
This converts the number to a string and sums digits recursively by processing characters, useful if input is already a string.

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.

ApproachTimeSpaceBest For
RecursiveO(d)O(d)Learning recursion and simple code
IterativeO(d)O(1)Performance and memory efficiency
String ConversionO(d)O(d)When input is a string or for character processing
💡
Always define a clear base case in recursion to avoid infinite calls.
⚠️
Beginners often forget to reduce the number in the recursive call, causing infinite recursion.