ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The loop below multiplies i by 3 each time while i is less than 1000, starting from i = 1, and counts the iterations. What is printed?

The loop below multiplies i by 3 each time while i is less than 1000, starting from i = 1, and counts the iterations. What is printed?
i = 1; count = 0
while i < 1000
    i = i * 3; count = count + 1
end while
Print count
  1. A. 7
  2. B. 333
  3. C. 10
  4. D. 5

Correct answer: A. 7

The values 1, 3, 9, 27, 81, 243 and 729 each start an iteration, which is 7 iterations, and 2187 is past 1000. Adding 3 instead of multiplying gives 333 iterations, doubling gives 10, and stopping at 100 gives 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic