In this article, we have shared a simple Python program to add the digits of a number using while loop. Once you execute this program it will ask to enter a positive number and the program then adds the digits of that number using while loop.
Copy the following python program and execute it to see the real time output.
# Python program to add digits of a number.
num = int(input("Enter a Number: "))
result = 0
hold = num
# while loop to iterate through all the digits of input number
while num > 0:
rem = num % 10
result = result + rem
num = int(num/10)
# displaying output
print("Sum of all digits of", hold, "is: ", result)
Enter a Number: 1200
Sum of all digits of 1200 is: 3
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.