In this program, we will calculate the sum of digits of a number.
Steps to solve the program
- Take a number as input through the user.
- Create a variable total and assign its value equal to 0.
- Using a while loop find the sum of digits of a number.
- While the number is greater than 0 divide the number by 10 using % and store the remainder in a variable.
- Add the remainder to the total variable.
- Now divide the number by 10 using //.
- Print the final output.
num = int(input("Enter a number: "))
total = 0
while num > 0:
rem = num%10
total = total + rem
num = num // 10
print("Sum of given number is: ",total)
Output :
Enter a number: 256
Sum of given number is: 13