Home » Sum of Digits using Function in Python

# Sum of Digits using Function in Python

Learn about Sum of Digits using Function in Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

#### Sum of Digits using Function in Python

Initially, the number is taken from the user input. Then function ComputeSum() in the below program calculates the sum of digits of the given number and returns a final count.

Source code:

``````# Python program to compute sum of digits in number

def ComputeSum(num):
sum = 0
while (num != 0):
sum += (num % 10)
num //= 10

return sum

# input
num = int(input('Enter a number: '))

# function call
print('Sum of digits in number =', ComputeSum(num))``````

Output:

Enter a number: 12345
Sum of digits in number = 15

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Sum of Digits of a Number in Python