Python Program to find the sum of digits of a number | Using while loop

Python Program to find the sum of digits of a number | Using while loop

Python Program to find the sum of digits of a number
In this example, we will learn how to find the sum of all digits of a number in Python. Our program will take one number as input from the user, calculate the sum of its digits and print it out. We will use while loop to find the digits of the given number and use one variable to store the sum of all the digits.
For example if user enters the the number 4538, then output i.e. sum of digits will be equal to 20. (4+5+3+8=20)

Python Program to find the sum of digits of a number
Number = int(input("Please Enter any Number: "))
Sum = 0
while(Number > 0):
    Reminder = Number % 10
    Sum = Sum + Reminder
    Number = Number //10
print("Sum of the digits of Given Number = %d" %Sum)

Output:
Please Enter any Number: 4538 Sum of the digits of Given Number = 20

Run Code: If you want run this code copy this code, paste here and run.

No comments: