ProgrammingVery hardProduct company OA

What will be the output of the following pseudocode? (/ is integer division.)

What will be the output of the following pseudocode? (/ is integer division.)
Integer c = 0, i = 1, j
while (i < 1000)
    j = i
    while (j > 0)
        j = j / 3
        c = c + 1
    end while
    i = i * 2
end while
Print c
  1. A. 40
  2. B. 10
  3. C. 34
  4. D. 45

Correct answer: C. 34

i takes 1, 2, 4, …, 512 (10 values). The inner loop counts the base-3 digits of i, i.e. ⌊log₃ i⌋ + 1: 1, 1, 2, 2, 3, 4, 4, 5, 6, 6 (e.g. 32 = 1012₃ has 4 digits, 512 = 200222₃ has 6). Sum 34. '10' counts only the outer iterations.

More Pseudocode and Code Tracing questions

Exams that ask this topic