ProgrammingHardTCS Digital · Infosys SP

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer c = 0, i = 1, j
while (i < 1000)
    j = i
    while (j > 0)
        c = c + 1
        j = j / 2
    end while
    i = i * 3
end while
Print c
  1. A. 35
  2. B. 37
  3. C. 28
  4. D. 42

Correct answer: B. 37

i runs through 1, 3, 9, 27, 81, 243, 729. The inner loop runs once per binary digit of i: 1, 2, 4, 5, 7, 8, 10 times. Total c = 37.

More Pseudocode and Code Tracing questions

Exams that ask this topic