Python Program to Find the Sum of Digits of a Number

Here we will write a program in Python to find the sum of Digits of a Number provided using while loop. Lets start with the steps for solution.

Step-wise Solution:

1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and add the digits to a variable.
3. Print the sum of the digits of the number.
4. Exit.

Now lets write the code for the same.

Python Program to Find the Sum of Digits of a Number:

# Python program to find sum of digits of a number

n=int(input("Enter a number:"))
tot=0
while(n>0):
    dig=n%10
    tot=tot+dig
    n=n/10
print("The total sum of digits is:",tot)

 

OUTPUT:

Enter a number:1892
The total sum of digits is: 20

Comment for any suggestions, concerns.

Leave a Comment