# Python Program to Calculate the Sum of Digits

In this tutorial, we will discuss a Python program to calculate the sum of the digits of a given number.

Before going to the program first, let us understand what is a Sum of Digits.

The Sum of Digits:

• We can obtain the sum of digits by adding the digits of a number by ignoring the place values.
• For example, the sum of the digits of 1234 is 1+2+3+4=10.

#### Program code to calculate the sum of digits using Python

```# Sum of Digits in Python
def sum_of_digits(num):
return sum(int(digit) for digit in str(num))

number = int(input("Enter a number: "))
print(f"The sum of the digits of {number} is {sum_of_digits(number)}.")

```

#### Explanation

1. Function Definition: The `sum_of_digits` function takes an integer `num` as input and returns the sum of its digits.
2. Sum Calculation: The function converts the number to a string, iterates through each digit, converts it back to an integer, and calculates the sum.
3. Main Program: The program prompts the user to enter a number and then calculates the sum of its digits using the `sum_of_digits` function.

#### Output

• When you run the above program, it will prompt you to enter a number.
• After entering the number, it will calculate the sum of its digits and print the result.

#### Conclusion

• In this tutorial, we learned how to calculate the sum of the digits of a given number using a Python program.
• Understanding this concept is essential for solving various mathematical problems and competitive programming challenges.
• Practice this example to enhance your programming skills and understanding of digit operations.