Calculate the sum of digits of a number

In this program, we will calculate the sum of digits of a number.

Steps to solve the program
  1. Take a number as input through the user.
  2. Create a variable total and assign its value equal to 0.
  3. Using a while loop find the sum of digits of a number.
  4. While the number is greater than 0 divide the number by 10 using and store the remainder in a variable.
  5. Add the remainder to the total variable.
  6. Now divide the number by 10 using //.
  7. 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
				
			

find the sum of the first and last digits of a number

calculate the product of digits of a number

Leave a Comment