What is the output of this code? With explain please. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the output of this code? With explain please.

n, i, cont 10, 10, 0 while(n // i == 1): i= i-1 cont = cont + 1 print(cont)

21st Dec 2018, 4:30 PM
Ahmad Ali
Ahmad Ali - avatar
2 Answers
+ 2
I think the program is like this, where cont is count for counting iterations n = 10 i = 10 cont = 0 while(n // i == 1): i= i-1 cont = cont + 1 print(cont) Now " // " is floor division operator and returns integer value of quotient for example, 10//3 will give 3 3//4 will give 1 About your program, 10 which is "n" is divided by "i" and if the quotient is 1 then only the while loop will run and in the while loop the i is decremented by one and cont is incremented by 1. 1st iteration 10//10 is 1, therefore loop will run. new value of i=9 new value of cont = 1 2nd iteration 10//9 is 1 therefore loop will run new value of i = 8 new value of cont = 2 3rd iteration 10//8 is 1, therefore loop will run. new value of i=7 new value of cont = 3 4th iteration 10//7 is 1, therefore loop will run. new value of i=6 new value of cont = 4 now 10//5 is 2 therefore loop will not run hence the output is 5
21st Dec 2018, 5:44 PM
Mayank Dhillon
Mayank Dhillon - avatar
+ 1
Thank you very much Mayank Dhillon
21st Dec 2018, 5:49 PM
Ahmad Ali
Ahmad Ali - avatar