0
0
CsharpProgramBeginner · 2 min read

C# Program for Sum of Digits Using Recursion

You can find the sum of digits in C# using recursion by defining a method like int SumOfDigits(int n) { if (n == 0) return 0; return n % 10 + SumOfDigits(n / 10); } which adds the last digit to the sum of the remaining digits recursively.
📋

Examples

Input123
Output6
Input0
Output0
Input9999
Output36
🧠

How to Think About It

To find the sum of digits using recursion, think of breaking the number into its last digit and the rest. Use the remainder operator % to get the last digit and integer division / to remove it. Add the last digit to the sum of digits of the remaining number by calling the same function again until the number becomes zero.
📐

Algorithm

1
Check if the number is zero; if yes, return zero as the sum.
2
Extract the last digit of the number using modulus operator.
3
Call the function recursively with the number divided by 10 (removing the last digit).
4
Add the last digit to the result of the recursive call.
5
Return the total sum.
💻

Code

csharp
using System;
class Program {
    static int SumOfDigits(int n) {
        if (n == 0) return 0;
        return n % 10 + SumOfDigits(n / 10);
    }
    static void Main() {
        int number = 123;
        Console.WriteLine("Sum of digits: " + SumOfDigits(number));
    }
}
Output
Sum of digits: 6
🔍

Dry Run

Let's trace the number 123 through the recursive sum of digits function.

1

Initial call

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

2

Second call

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

3

Third call

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

4

Base case

SumOfDigits(0) returns 0

5

Returning sums

SumOfDigits(1) returns 1 + 0 = 1 SumOfDigits(12) returns 2 + 1 = 3 SumOfDigits(123) returns 3 + 3 = 6

CallNumberLast DigitRecursive CallReturn Value
11233SumOfDigits(12)6
2122SumOfDigits(1)3
311SumOfDigits(0)1
400Base case0
💡

Why This Works

Step 1: Base Case

The recursion stops when the number becomes zero, returning 0 because there are no digits left to add.

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 n / 10 to process the remaining digits, building the sum step by step.

🔄

Alternative Approaches

Using a loop instead of recursion
csharp
using System;
class Program {
    static int SumOfDigits(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
    static void Main() {
        int number = 123;
        Console.WriteLine("Sum of digits: " + SumOfDigits(number));
    }
}
This approach uses iteration and is often easier to understand for beginners; it avoids the overhead of recursive calls.
Convert number to string and sum digits
csharp
using System;
class Program {
    static int SumOfDigits(int n) {
        int sum = 0;
        foreach (char c in n.ToString()) {
            sum += c - '0';
        }
        return sum;
    }
    static void Main() {
        int number = 123;
        Console.WriteLine("Sum of digits: " + SumOfDigits(number));
    }
}
This method converts the number to a string and sums the digits by converting characters back to integers; it is simple but less efficient.

Complexity: O(log n) time, O(log n) space

Time Complexity

The function calls itself once per digit, so the time grows with the number of digits, which is proportional to log base 10 of the number.

Space Complexity

Each recursive call adds a layer to the call stack, so space used is proportional to the number of digits (log n).

Which Approach is Fastest?

The iterative approach is faster and uses less memory because it avoids recursive call overhead, but recursion is elegant and easy to understand.

ApproachTimeSpaceBest For
RecursionO(log n)O(log n)Learning recursion, elegant code
Iteration (loop)O(log n)O(1)Performance and memory efficiency
String conversionO(log n)O(log n)Simple code, less efficient
💡
Use recursion only when you want to practice or when the problem naturally fits recursive thinking.
⚠️
Forgetting the base case causes infinite recursion and crashes the program.