QUESTION IMAGE
Question
sum of digits
by codechum admin
write a program that takes an integer input from
the user and calculates the sum of its digits
using a while loop. the program should output
the result to the user.
sample output 1
enter an integer: 51
sum of digits: 6
sample output 2
enter an integer: 120
sum of digits: 3
sample output 3
enter an integer: 1562
sum of digits: 14
Step1: Initialize variables
num = int(input("Enter an integer: "))
sum_digits = 0
Step2: Use while loop to calculate sum
while num > 0:
digit = num % 10
sum_digits += digit
num = num // 10
Step3: Print the result
print("Sum of digits:", sum_digits)Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
num = int(input("Enter an integer: "))
sum_digits = 0
while num > 0:
digit = num % 10
sum_digits += digit
num = num // 10
print("Sum of digits:", sum_digits)