ProgrammingHardTCS NQT · Infosys

What will be the output of the following pseudocode? (All variables are integers, so / is integer division; and / are evaluated left to right)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division; * and / are evaluated left to right)
Integer p = 1000, years = 0
while (p < 2000)
    p = p + p * 7 / 100
    years = years + 1
end while
Print years, p
  1. A. 10 1967
  2. B. 10 2000
  3. C. 11 2098
  4. D. 12 2245

Correct answer: C. 11 2098

Each year adds 7% rounded down: 1070, 1144, 1224, 1309, 1400, 1498, 1602, 1714, 1833, 1961, 2098. The amount first reaches 2000 after 11 years: 11 2098 (the rule of 72 suggests about 10).

More Pseudocode and Code Tracing questions

Exams that ask this topic