C# program to find the sum of digits of a number using Recursion

A recursive function calls itself with modified parameters until it reaches a base case. To find the sum of digits of a number using recursion, we extract the last digit using the modulus operator (%) and add it to the sum of remaining digits.

Syntax

Following is the syntax for a recursive function to sum digits −

public int SumOfDigits(int number) {
   if (number == 0) {
      return 0;  // base case
   }
   return (number % 10) + SumOfDigits(number / 10);
}

How It Works

The recursive approach breaks down the problem as follows −

  • Base case: When the number becomes 0, return 0

  • Recursive case: Extract the last digit using number % 10 and add it to the sum of remaining digits

  • Each recursive call works with number / 10, effectively removing the last digit

Recursive Sum of Digits: 789 789 % 10 = 9 78 % 10 = 8 7 % 10 = 7 + Sum(78) + Sum(7) + Sum(0) Result: 9 + 8 + 7 = 24

Example

The following program demonstrates finding the sum of digits using recursion −

using System;

class DigitSum {
   public int SumOfDigits(int number) {
      if (number == 0) {
         return 0;
      }
      return (number % 10) + SumOfDigits(number / 10);
   }
}

class Program {
   static void Main() {
      DigitSum calculator = new DigitSum();
      int number = 789;
      
      Console.WriteLine("Number: " + number);
      int result = calculator.SumOfDigits(number);
      Console.WriteLine("Sum of digits in " + number + " = " + result);
   }
}

The output of the above code is −

Number: 789
Sum of digits in 789 = 24

Using Static Method

You can also implement the recursive function as a static method for simpler usage −

using System;

class Program {
   static int SumOfDigits(int number) {
      if (number == 0) {
         return 0;
      }
      return (number % 10) + SumOfDigits(number / 10);
   }
   
   static void Main() {
      int num1 = 1234;
      int num2 = 56789;
      
      Console.WriteLine("Number: " + num1);
      Console.WriteLine("Sum of digits: " + SumOfDigits(num1));
      
      Console.WriteLine("Number: " + num2);
      Console.WriteLine("Sum of digits: " + SumOfDigits(num2));
   }
}

The output of the above code is −

Number: 1234
Sum of digits: 10
Number: 56789
Sum of digits: 35

Handling Negative Numbers

To handle negative numbers, we can use the absolute value before processing −

using System;

class Program {
   static int SumOfDigits(int number) {
      number = Math.Abs(number);
      if (number == 0) {
         return 0;
      }
      return (number % 10) + SumOfDigits(number / 10);
   }
   
   static void Main() {
      int positiveNum = 456;
      int negativeNum = -456;
      
      Console.WriteLine("Sum of digits in " + positiveNum + " = " + SumOfDigits(positiveNum));
      Console.WriteLine("Sum of digits in " + negativeNum + " = " + SumOfDigits(negativeNum));
   }
}

The output of the above code is −

Sum of digits in 456 = 15
Sum of digits in -456 = 15

Conclusion

Recursion provides an elegant solution for finding the sum of digits by breaking the problem into smaller subproblems. The recursive function extracts the last digit and adds it to the sum of remaining digits until the base case (number = 0) is reached.

Updated on: 2026-03-17T07:04:35+05:30

451 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements