sum of digits\nby codechum admin\nwrite a program that takes an integer input from\nthe user and calculates…

sum of digits\nby codechum admin\nwrite a program that takes an integer input from\nthe user and calculates the sum of its digits\nusing a while loop. the program should output\nthe result to the user.\nsample output 1\nenter an integer: 51\nsum of digits: 6\nsample output 2\nenter an integer: 120\nsum of digits: 3\nsample output 3\nenter an integer: 1562\nsum of digits: 14
Answer
Explanation:
Step1: Get user input
num = int(input("Enter an integer: "))
Step2: Initialize sum
sum_digits = 0
Step3: Use while loop to calculate sum
while num > 0:
digit = num % 10
sum_digits += digit
num = num // 10
Step4: Output the result
print(f"Sum of digits: {sum_digits}")
Answer:
num = int(input("Enter an integer: "))
sum_digits = 0
while num > 0:
digit = num % 10
sum_digits += digit
num = num // 10
print(f"Sum of digits: {sum_digits}")