C# Program to Find Sum of Digits of a Number Using Recursion Last Updated : 22 Apr, 2022 Comments Improve Suggest changes Like Article Like Report Given a number, we need to find the sum of digits in the number using recursion. In C#, recursion is a process in which a function calls itself directly or indirectly and the corresponding function is known as a recursive function. It is used to solve problems easily like in this article using recursion we will find the sum of digits of a number. Example Input: 456 Output: 15 Input: 123 Output: 6 Approach: To find the sum of digits of a number using recursion follow the following approach: We call the SumOfDigits function with the argument n.In function, the last digit is retrieved by n % 10.The function is recursively called with argument as n / 10.[i.e. n % 10 + SumOfDigit(n / 10)]The function is called recursively until the n > 0. Example 1: C# // C# program to find the sum of digits of a number // using recursion using System; class GFG{ // Method to check sum of digit using recursion static int SumOfDigit(int n) { // If the n value is zero then we // return sum as 0. if (n == 0) return 0; // Last digit + recursively calling n/10 return(n % 10 + SumOfDigit(n / 10)); } // Driver code public static void Main() { int n = 123; int ans = SumOfDigit(n); Console.Write("Sum = " + ans); } } OutputSum = 6 Example 2: C# // C# program to find the sum of digits of a number // using recursion // Here, we take input from user using System; class GFG{ // Method to check sum of digit using recursion static int SumOfDigit(int n) { // If the n value is zero then we return sum as 0. if (n == 0) return 0; // Last digit + recursively calling n/10 return (n % 10 + SumOfDigit(n / 10)); } // Driver code public static void Main() { int number, res; // Taking input from user Console.WriteLine("Hi! Enter the Number: "); number = int.Parse(Console.ReadLine()); res = SumOfDigit(number); // Displaying the output Console.WriteLine("Sum of Digits is {0}", res); Console.ReadLine(); } } Output: Hi! Enter the Number: 12345 The Sum of Digits is 15 Comment More info R raghu135ram Follow Improve Article Tags : C# CSharp-programs Explore IntroductionC# Tutorial 4 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsClass and Objects in C# 4 min read Constructors in C# 5 min read C# Inheritance 3 min read Encapsulation in C# 2 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read C# | Method Parameters 7 min read Method Overriding in C# 7 min read Anonymous Method in C# 3 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read C# ArrayList Class 7 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 4 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 4 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like