CBSE Class 11 Informatics Practices Question 29 of 36

Conditional and Looping Constructs — Question 29

Back to all questions
29
Question

Question 13

WAP to find the sum of the digits of a 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

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