Learn about the **Sum of Digits of a Number 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 of a Number in Python

A while loop is used in this Python program to compute the sum of digits in an integer. We have used a function in the below code. A function is a piece of code that accomplishes a specified goal.

When declaring the variables, we shall use integer numbers. Python program that computes the sum of digits using a while loop and displays the result on the screen.

**Program:**

```
# Python program to compute sum of digits in number
#user-defined function
def ComputeSum(num):
sum = 0
while (num != 0):
sum += (num % 10)
num //= 10
return sum
# input from user
num = int(input('Enter a number: '))
# function call
print('Sum of digits in number =', ComputeSum(num))
```

**Output**:

Enter a number: 123

Sum of digits in number = 6

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

**Similar Code :** Python Program to Solve Quadratic Equation using cmath Module