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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Largest number less than N with digit sum greater than the digit sum of N
- Count of N-digit numbers having digit XOR as single digit
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- 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
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Finding sum of digits of a number until sum becomes single digit
- Check if frequency of each digit is less than the digit
- Count n digit numbers not having a particular digit
- How to solve problems related to Number-Digits using Recursion?
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Maximum sum of two elements whose digit sum is equal
- Sum of M maximum distinct digit sum from 1 to N that are factors of K
- Sum and Product of all even digit sum Nodes of a Singly Linked List
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Queries for count of even digit sum elements in given range using MO's Algorithm