Python Program to Add Digits of Number using Recursion

Python Program to Add Digits of Number using Recursion

Write Python Program to Add Digits of Number using Recursion

# Write Python Program to Add Digits of Number using Recursion

def sum_digits(n):
    result = 0
    if n == 0:
        result = 0
    else:
        result = (n % 10) + sum_digits(n // 10)
    return result

n = 12345
Sum = sum_digits(n)
print(f'Sum of digits of number {n}: {Sum}')

Output:

Sum of digits of number 12345: 15