Python Program to find the sum of digits of a number using recursion
In this program, you will learn how to find the sum of digits of a number using recursion in Python.
4321 => 4 + 3 + 2 + 1
1232 => 1 + 2 + 3 + 2
Example: How to find the sum of digits of a number using recursion in Python
s = 0
def sumofdigits(n):
global s
if n > 0:
r = n % 10
s = s + r
sumofdigits(int(n / 10))
return s
n = int(input("Enter a number:"))
num = sumofdigits(n)
print("Sum of digits:", num)
Output:
Enter a number:123
Sum of digits: 6