Python Program to find the sum of digits of a number using the function


In this program, You will learn how to find the sum of digits of a number using a function in Python.


1234 => 1 + 2 + 3 + 4 = 10

Example: How to find the sum of digits of a number using a function in Python.

def reverse(n):
    s = 0

    while n > 0:
        r = n % 10
        s = s + r
        n = int(n / 10)

    return s


x = int(input("Enter a number:"))
result = reverse(x)
print("Sum of all digits:", result)

Output:

Enter a number:2345
Sum of all digits: 14