+ 1

Help!!!

The given program calculates and outputs the number of digits in a given number using a while loop. n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length) PY During each iteration, the loop uses floor division to divide the given number by 10, thus dropping one digit. The process continues until the number has no more digits (n>0). You need to change the code to calculate and output the sum of all digits of the input number.

1st Mar 2022, 8:14 AM
J1mbo Show
2 Answers
+ 2
J1mbo Show Another hint You will need another variable to catch the result. result = 0 Then as you get the remainder from each % iteration, you could add it to the result using +=
1st Mar 2022, 9:12 AM
Rik Wittkopp
Rik Wittkopp - avatar