ProgrammingHardInfosys · Capgemini pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer x = 100, c = 0
while (x > 1)
    if (x mod 2 == 0) x = x / 2
    else x = 3 * x + 1
    c = c + 1
end while
Print c
  1. A. 24
  2. B. 26
  3. C. 7
  4. D. 25

Correct answer: D. 25

The Collatz sequence from 100: 100, 50, 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 – 25 steps. Counting values instead of steps gives 26.

More Pseudocode and Code Tracing questions

Exams that ask this topic