What is the output of the following i=1 while true if i%2==0 break print (i) i
+=2
Answers
Answered by
1
The output will be 1 3 5 7 9 11.... which means the output will be in an infinite loop.
- Since. i=1 and the condition is while(true) which means the loop will be executed. Now, checking the condition i%2==0.
- 1%2==0 which means if 1 is divided by 2 then is the remainder 0. Since the remainder will be 1, the break statement will be not executed and the value of i will be printed i.e. 1.
- Now i+=2 means i is incremented by 2 , i becomes 1+2= 3. Now, again the loop will be executed as while(true). Again the condition is checked.
- 3%2==0 and this condition is again false as 3%2 is 1. So, again break statement will be not executed and the value of i will be printed i.e. 3.
- Now again the value of i is incremented by 2 and i becomes 5. If we notice the break statement will be executed only if the value of i is divisible by 2 which means if i is an even number.
- But i will always be an odd number. So, the output will be in an infinite loop.
So, the output will be 1 3 5 7 9 11 15 .... entered into infinite loop.
#SPJ3
Answered by
0
The result will be 1 3 5 7 9 11, which indicates that the output will loop indefinitely.
- Since, The loop will be executed because i=1 and the condition is while(true). Now, verify that I percent 2==0 is true.
- 1 percent 2==0, which indicates that if you divide 1 by 2, the remainder is 0. The break statement will not be executed because the remainder will be 1, hence the value of I will be printed, which is 1.
- Now, i+=2 signifies that I has been increased by 2, and it is now 1+2=3. The loop will now be executed as a while loop once again (true). The condition is verified once again.
- 3%2 = 0 and this condition is again false as 3%2 is 1. So, again break statement will be not executed and the value of i will be printed i.e. 3.
- Now again the value of i is incremented by 2 and i becomes 5. If we notice the break statement will be executed only if the value of i is divisible by 2 which means if i is an even number.
- But i will always be an odd number. So, the output will be in an infinite loop.
What will be the output of the following Python code import time time Asctime ()?
- The function time asctime(), present if the time module can be used to return the current date and time.
- It can also accept a parameter and return the date and time in a particular format.
What is the output of print 0.1 0.2 == 0.3 )?
- Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary.
- The round off errors from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17 between (0.1 + 0.2) and 0.3.
Hence, the output will be 1 3 5 7 9 11 15 .... entered into infinite loop.
#SPJ2
Similar questions