Sum of digit of a number using recursion Last Updated : 17 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Given a number, we need to find sum of its digits using recursion.Examples: Input: 12345Output: 15Explanation: Sum of digits → 1 + 2 + 3 + 4 + 5 = 15Input: 45632Output: 20 Approach:To understand the algorithm, consider the number 12345 and refer to the illustration below..Extract the last digit: 12345 % 10 = 5, pass 1234 to the next step.Extract next digit: 1234 % 10 = 4, pass 123 to the next step.Extract next digit: 123 % 10 = 3, pass 12 to the next step.Extract next digit: 12 % 10 = 2, pass 1 to the next step.Extract last digit: 1 % 10 = 1, pass 0, stopping recursion.Each step adds the extracted digit to the sum, forming 1 + 2 + 3 + 4 + 5 = 15 Recursive Sum of Digits for 12345Note: Instead of if (n == 0) return 0;, we can use if (n < 10) return n;, eliminating extra function calls for single-digit numbers without changing the output. C++ // Recursive C++ program to find sum of digits // of a number #include <bits/stdc++.h> using namespace std; // Function to check sum of digit using recursion int sum_of_digit(int n) { if (n == 0) return 0; return (n % 10 + sum_of_digit(n / 10)); } // Driven code int main() { int num = 12345; int result = sum_of_digit(num); cout << result << endl; return 0; } C // Recursive C program to find sum of digits // of a number #include <stdio.h> // Function to check sum of digit using recursion int sum_of_digit(int n) { if (n == 0) return 0; return (n % 10 + sum_of_digit(n / 10)); } // Driven Program to check above int main() { int num = 12345; int result = sum_of_digit(num); printf("%d", result); return 0; } Java import java.io.*; class GfG { // Function to check sum // of digit using recursion static int sum_of_digit(int n) { if (n == 0) return 0; return (n % 10 + sum_of_digit(n / 10)); } // Driven Program to check above public static void main(String args[]) { int num = 12345; int result = sum_of_digit(num); System.out.println(result); } } Python # Function to check sum of # digit using recursion def sum_of_digit( n ): if n == 0: return 0 return (n % 10 + sum_of_digit(int(n / 10))) # Driven code to check above num = 12345 result = sum_of_digit(num) print(result) C# using System; class GfG { // Function to check sum // of digit using recursion static int sum_of_digit(int n) { if (n == 0) return 0; return (n % 10 + sum_of_digit(n / 10)); } // Driven Program to check above public static void Main() { int num = 12345; int result = sum_of_digit(num); Console.WriteLine(result); } } JavaScript // Function to check sum of digit using recursion function sum_of_digit(n) { if (n == 0) return 0; return (n % 10 + sum_of_digit(parseInt(n / 10))); } // Driven code var num = 12345; var result = sum_of_digit(num); console.log(result ); Output15 Besides writing (n==0 , then return 0) in the code given above we can also write it in this manner , there will be no change in the output .if (n <1 0) return n; by writing this there will be no need to call the function for the numbers which are less than 10 Time Complexity: O(log10n), Traverse through all the digits, as there are log10n bits.Auxiliary Space: O(log10n), due to recursive function calls stored in the call stack. Comment More infoAdvertise with us Next Article Sum of digit of a number using recursion S Shahnawaz_Ali Follow Improve Article Tags : Misc Recursion DSA number-digits Practice Tags : MiscRecursion Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++ // C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std; // ----- Recursion 6 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recur 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Like