Python Program to Add Digits of Number

« Previous Program Next Program »

Add Digits of Number in Python

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.

Python Programming Code to Add Digits of 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':
		number = int(num)
	except ValueError:
		print("Please, enter a number...")
		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:

python add digits of number

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:

« Previous Program Next Program »

Latest 5 Posts by CodesCracker


Quick Links
Signup - Login - Give Online Test