Guys how can I do this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Guys how can I do this

n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length) 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. Sample Input 643 Sample Output 13 Explanation The sum of the digits of 643 is 6+4+3 = 13.

15th May 2021, 10:51 AM
Avinash Maharoliya
Avinash Maharoliya - avatar
2 Answers
+ 2
You have to add reminder after every iteration of loop so do like this: n = int(input()) sum = 0 while n > 0: sum += n % 10 n = n // 10 print(sum)
15th May 2021, 10:57 AM
A͢J
A͢J - avatar
15th May 2021, 12:38 PM
Endalk
Endalk - avatar