Python Program to Find the Sum of the Digits of the Number Recursively

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Given a number the task is to calculate the sum of the digits of the given number using recursive approach in Python.

Examples:

Example1:

Input:

Enter some random number = 18627677851

Output:

The total sum of digits off the given number 18627677851 = 58

Example2:

Input:

Enter some random number = 7816833887102099

Output:

The total sum of digits off the given number 7816833887102099 = 80

Program to Find the Sum of the Digits of the Number Recursively

Below are the ways to calculate the sum of the digits of the given number using recursive approach in Python.

1)Using Recursion(Static Input)

Approach:

• Create a recursive function that accepts a number as an argument.
• Take a number from the static input and pass it to a recursive function as an argument.
• Put the base condition in the function that says if the number is zero, return the created list.
• Otherwise, take each digit and add it to the list.
• Outside of the function, find the sum of the digits in the list.
• Print the total sum
• Exit of program.

Below is the implementation:

# take a empty list
numbList = []
# function which returns count of all the digits of
# the given number using recursive approach.

def sumDigitsRecursion(numb):
# Put the base condition in the function that says
# if the number is zero, return the created list.
if(numb == 0):
return numbList
# getting the last digit of the given number using modulus operator
numdigit = numb % 10
# appending this digit to numberslist using digit function
numbList.append(numdigit)
# passing numb/10 recursively
sumDigitsRecursion(numb//10)

# give the number as static input
numb = 18627677851
# passing the number to sumDigitsRecursion function to
# calculate the sum of digits recursively
sumDigitsRecursion(numb)
# calculating the sum of list using sum() function.
print('The total sum of digits off the given number', numb, '=', sum(numbList))


Output:

The total sum of digits off the given number 18627677851 = 58

2)Using Recursion(User Input)

Approach:

• Create a recursive function that accepts a number as an argument.
• Take a number from the user input by using int(input()) function and pass it to a recursive function as an argument.
• Put the base condition in the function that says if the number is zero, return the created list.
• Otherwise, take each digit and add it to the list.
• Outside of the function, find the sum of the digits in the list.
• Print the total sum
• Exit of program.

Below is the implementation:

# take a empty list
numbList = []
# function which returns count of all the digits of
# the given number using recursive approach.

def sumDigitsRecursion(numb):
# Put the base condition in the function that says
# if the number is zero, return the created list.
if(numb == 0):
return numbList
# getting the last digit of the given number using modulus operator
numdigit = numb % 10
# appending this digit to numberslist using digit function
numbList.append(numdigit)
# passing numb/10 recursively
sumDigitsRecursion(numb//10)

#scan some random number using int(input()) function
numb = int(input('Enter some random number = '))
# passing the number to sumDigitsRecursion function to
# calculate the sum of digits recursively
sumDigitsRecursion(numb)
# calculating the sum of list using sum() function.
print('The total sum of digits off the given number', numb, '=', sum(numbList))


Output:

Enter some random number = 7816833887102099
The total sum of digits off the given number 7816833887102099 = 80

Explanation:

• A recursive function with a number as an argument is defined.
• A user-given number is taken and sent as an argument to a recursive function.
• The base condition of the function is that if the number is zero, the created list is returned.
• Otherwise, each digit is acquired through the use of a modulus operator and appended to the list.
• The function is then invoked with the user’s number, and the total of the digits in the list is calculated.
• The total sum is printed.

Related Programs: