ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The Fibonacci sequence 1, 1, 2, 3, 5, ... is generated by the loop below, and the procedure prints how many terms are less than or equal to 100. What is printed?

The Fibonacci sequence 1, 1, 2, 3, 5, ... is generated by the loop below, and the procedure prints how many terms are less than or equal to 100. What is printed?
a = 1; b = 1; count = 0
while a <= 100
    count = count + 1
    (a, b) = (b, a + b)
end while
Print count
  1. A. 12
  2. B. 100
  3. C. 11
  4. D. 10

Correct answer: C. 11

The terms counted are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 and 89, which is 11 terms. Starting from 0 adds the term 0, a broken update of the pair gives a different sequence, and testing the second variable shifts the count.

More Pseudocode and Code Tracing questions

Exams that ask this topic