224 views

2 Answers

0 votes
0 votes
option(c) is correct

let the value of n=4 and queue is empty initially.

now we insert 0 and 1 in the queue

value of i is initialized to 0.there are two variables x and y,so in the first iteration delete the elements from queue and put 0 and 1 into the x and y respectively.Now insert the value of y and x+y means 1 and 1 into the queue and print x means 0.

now i is incremented by 1 and becomes 1 (i<4) so we'll perform second iteration ,in second iteration we delete elements from queue and put it into the x and y respectively so now x and y will have values 1 and 1 respectively,now place these value means y and x+y into the queue means 1 and 2 into the queue and print the value of x as 1.

now i is incremented by 1 and becomes 2 (i<4),now we will perform 3rd iteration so delete elements from queue and put it into the x and y respectively means x and y will have 1 and 2 respectively,now put the value of y and x+y into the queue means 2 and 3 in same order and print the value of x =>1

now i is incremented by 1 and becomes 3(i<4) no we will perform 4th iteration so delete elements from  queue and put it into x and y respectively means x and y will have 2 and 3 ,now put values of y and x+y into the queue means 3 and 5 into the queue and print the value of x =>2

now i is incremented by 1 and becomes 4 (i!<4) condition false so will come out of the loop

so values are printed as 0112 for n=4 so we can clearly see that it is following fibonacci series
0 votes
0 votes

Before the iteration begins, 0 and 1 are entered into the queue so that Q = 1,0.

Now let us go through the iterations and find the output. Say n=7.

 

Iteration x=dequeue(Q) y=dequeue(Q) Qnow

Qafter

enqueue(Q,y) and

enqueue(Q,x+y)

Output

(Print x)

i=0 0 1 empty 1,1 0
i=1 1 1 empty 2,1 01
i=2 1 2 empty 3,2 011
i=3 2 3 empty 5,3 0112
i=4 3 5 empty 8,5 01123
i=5 5 8 empty 13,8 011235
i=6 8 13 empty 21,13 0112358

Therefore, the code prints the first n fibonacci numbers.
So, the answer is (C).

No related questions found