Write a Python program to calculate the sum of digits of a number.

SOLUTION.

# Taking dynamic input from the user
num = int(input("Enter a number: "))

# Calculating the sum of digits
sum_of_digits = sum(int(digit) for digit in str(abs(num)))  # Convert number to string, iterate over digits, and sum them

# Displaying the result
print("The sum of digits is:", sum_of_digits)

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.