Given a number, we need to find sum of its digits using recursion.
Input : 12345 Output : 15 Input : 45632 Output :20
Step by step process for better understanding of how the algorithm works.
Let number be 12345.
Step 1-> 12345 % 10 which is equal-too 5 + ( send 12345/10 to next step )
Step 2-> 1234 % 10 which is equal-too 4 + ( send 1234/10 to next step )
Step 3-> 123 % 10 which is equal-too 3 + ( send 123/10 to next step )
Step 4-> 12 % 10 which is equal-too 2 + ( send 12/10 to next step )
Step 5-> 1 % 10 which is equal-too 1 + ( send 1/10 to next step )
Step 6-> 0 algorithm stops
following diagram will illustrate the process of recursion
Sum of digits in 12345 is 15
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count the occurrence of digit K in a given number N using Recursion
- Add the given digit to a number stored in a linked list using recursion
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Perform n steps to convert every digit of a number in the format [count][digit]
- Count of Numbers in Range where first digit is equal to last digit of the number
- Find the remainder when First digit of a number is divided by its Last digit
- Last digit of a number raised to last digit of N factorial
- Largest number less than N with digit sum greater than the digit sum of N
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Count Set-bits of number using Recursion
- Decimal to binary number using recursion
- Program to find all Factors of a Number using recursion
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- How to solve problems related to Number-Digits using Recursion?
- Largest number less than N whose each digit is prime number
- Count the number of occurrences of a particular digit in a number
- Least Greater number with same digit sum
- Number of n digit numbers that do not contain 9
- Nth number whose sum of digit is multiple of 10
- Special two digit number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.