0

n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length)

can anyone explain this..?

17th Oct 2020, 2:46 AM
Rahul Prasad
Rahul Prasad - avatar
3 Answers
+ 3
It gets the digit of a number by division by 10. The reason of floor division is to get rid of the decimal. For example input 428 428 // 10 = 42 length = 1 42 // 10 = 4 length = 2 4 // 10 = 0 length = 3 0 > 0 is false, loop ends.
17th Oct 2020, 3:04 AM
CarrieForle
CarrieForle - avatar
+ 3
Your code is simple first you taking number which type will be int and u take another variable length which will cout the length of Entered number . Your loop will run upto >0 times if condition will false then you will exit from loop In next line you wrote n//=10 if you will divide number by 10 it will seperate your number and length += 1 will count total number. if u will run of rough copy you can understood much better
17th Oct 2020, 3:06 AM
ASR
2
which interpreter can run this as is and produce result or without an error?
20th Jan 2021, 7:47 AM
Kostas_V