## C# Program to Find the Sum of Digits of a Number

Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor.

This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.

Watch our videos here

#### Question:

How can you find the sum of digits of a given positive integer? Write a program for finding the sum of digits of a given number using recursion.

### C# Program To Find Sum of Digits of a Number

```
```

// Author 	: Tech Point Fundamentals
// Website 	: www.techpointfunda.com
// Demo		: Finding Sum of Digits

using System;

public class Program
{
public static void Main()
{
int inputNumber = 9845;

Console.WriteLine("Input Number : " + inputNumber);
Console.WriteLine("Sum of Digits : " + SumOfDigits(inputNumber));
Console.WriteLine("Sum of Digits using Recursion: " + DigitSumRecursion(inputNumber));
}

public static int SumOfDigits(int inputNumber)
{
int sum = 0;
while (inputNumber > 0)
{
sum += inputNumber % 10;
inputNumber /= 10;
}
return sum;
}

public static int DigitSumRecursion(int inputNumber)
{
if (inputNumber != 0)
{
return (inputNumber % 10 + DigitSumRecursion(inputNumber / 10));
}
else
{
return 0;
}
}
}

```
```

#### Output:

Input Number : 9845
Sum of Digits : 26
Sum of Digits using Recursion: 26

### Live Demo

#### Recommended Articles

SOLID Design Principles