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 infoAdvertise with us Next Article C# Program to Find Sum of Digits of a Number Using Recursion R raghu135ram Follow Improve Article Tags : C# CSharp-programs Similar Reads How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to 2 min read JavaScript Program for Sum of Digits of a Number using Recursion We are given a number as input and we have to find the sum of all the digits contained by it. We will split the digits of the number and add them together using recursion in JavaScript. Table of Content Recursively Summing DigitsUsing string manipulation with recursionRecursively Summing DigitsIn th 2 min read C Program to Find Sum of Natural Numbers using Recursion Natural numbers include all positive integers from 1 to infinity. There are multiple methods to find the sum of natural numbers and here, we will see how to find the sum of natural numbers using recursion. Example Input : 5Output : 15Explanation : 1 + 2 + 3 + 4 + 5 = 15 Input : 10Output : 55Explanat 2 min read C Program to Calculate Sum of Natural Numbers Here we will build a C program to calculate the sum of natural numbers using 4 different approaches i.e. Using while loopUsing for loopUsing recursionUsing Functions We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 10 Output: 55 Explanation: The s 3 min read How to Handle Large Numbers in C? In C, the maximum value that an integer type variable can store is limited. For instance, the maximum value that an long long int can hold in 64-bit compiler is 9223372036854775807. So, how can we handle numbers that are larger than this? In this article, we will learn how to handle large numbers in 2 min read Like