ProgrammingVery hardLTIMindtree · Cognizant GenC

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 9, steps = 0
while (n != 1)
    if (n mod 2 == 0)
        n = n / 2
    else
        n = 3 * n + 1
    end if
    steps = steps + 1
end while
Print steps
  1. A. 18
  2. B. 20
  3. C. 19
  4. D. 21

Correct answer: C. 19

The sequence 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 takes 19 steps.

More Pseudocode and Code Tracing questions

Exams that ask this topic