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.
2 Antworten
+ 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)