0
n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length)
can anyone explain this..?
4 odpowiedzi
+ 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.
+ 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
0
n=int(input(" enter:"))
i = 0
while n>0:
i += 1
n = n//10
print (i)
- 2
which interpreter can run this as is and produce result or without an error?