1.

Analyze the following program segment and determine how many times the body of loop will be executed (show the working). x=5; y=50; while (x<=y) { y=y/x; System.out.println(y); }

Answer»

Working of a loop.

Loop executionValue of x and yCondition (x<=y)Output
First time5 and 50Truey=50/5; i.e. 10 output=
Second time5 and 10Truey=10/5; i.e. 2 Output =
Third time5 and 2FalseExit the loop

Thus with the above table, it is clear that the loop will executed times and produces the output 10 and 2 respectively. On the third time it becomes false and the iteration stops.



Discussion

No Comment Found

Related InterviewSolutions