To add or sum all the digits of a number given by the user in python, first ask from user to enter the number, then break the given number into its digit and sum or add all the digit. Finally display the addition result of the digits of given number.
Following python program ask from the user to enter a number to find the sum of the digit of that number, then display the result as the output in python:
# Python Program - Add Digits of a Number while True: print("Enter 'x' for exit.") num = input("Enter any number: ") if num == 'x': break try: number = int(num) except ValueError: print("Please, enter a number...") else: sum = 0 temp = number while number > 0: rem = number % 10 sum += rem number //= 10 print("Sum of all digits of", temp, "is", sum, "\n")
Here is the snapshot of sample run of above python program:
You may also like to learn or practice the same program in other popular programming languages: