CBSE Class 12 Computer Science Question 82 of 120

Review of Python Basics — Question 34

Back to all questions
34
Question

Question 29

Write a Python program to compute sum of digits of a given number.

Solution
number = int(input("Enter a number: "))
sum_of_digits = 0
while number > 0:
    digit = number % 10
    sum_of_digits += digit
    number = number // 10

print("Sum of digits:", sum_of_digits)
Output
Enter a number: 4556787
Sum of digits: 42
Answer