2.4k views

Consider the following pseudo-code

x:=1;
i:=1;
while (x <= 1000)
begin
x:=2^x;
i:=i+1;
end;

What is the value of i at the end of the pseudo-code?

1. 4
2. 5
3. 6
4. 7
| 2.4k views
–1
Is it A.4
+1

I=1         n=1

2                 2

3                  2^2

4                      2^4

5                        2^16

Than loop terminate means i=5

1st time ... x = 2 and i = 2

2nd time ...x=4 and i=3

3rd time.....x=16 and i = 4

4th time ......before entering the loop value of x < 1000 ....it is 16 so it will enter the loop

and make i = 5

5th time x>1000 so while loop doesn't execute.....

SO the answer is i = 5
by Active (1k points)
selected by
0
4th time condition is false why..??

3rd tym also it is false na...? As 16 <1000..

????
0
4th time x becomes 2^16 becomes after the condition check.

So in 5th time the condition fails.( 2^16<1000)

Hence i=5.
0

what does "

while (x $\leq$ 1000)

means..??

0
@ kashyap avi      ...no value of i will be 5 check again
0
@dhairya ...while (x<=1000)
0
thank you ... it was my mistake
0
thanxx guys... I got it now...:D
 initialy 1st itr.. 1<=1000(true) 2nd itr..  2<=1000(true) 3rd itrn  ..4<=1000(true) 4th itrn   16<=1000(true) x=1 x=2 x=4 x=16 x=>1000 i=1 i=2 i=3 i=4 i=5

then in the next iteration while loop will be false

and value of i=5.

B is the ans

by Boss (11.1k points)
+1 vote
by (21 points)