Python Program To Find Sum Of Digit Of A Number Using Recursive Function


This Python program calculates sum of digit of a given number using recursion.


In this program, we firs read number from user and pass this number to recursive function sum_of_digit() which calculates sum of digit in a number.

Python Source Code: Sum of Digit Recursion


# Sum of digit of number using recursion

def sum_of_digit(n):
    if n< 10:
        return n
    else:
        return n%10 + sum_of_digit(n/10)

# Read number
number = int(input("Enter number: "))

# Function call
digit_sum = sum_of_digit(number)

# Display output
print("Sum of digit of number %d is %d." % (number,digit_sum))

Output

Enter number: 232
Sum of digit of number 232 is 7.