ProgrammingHardTCS NQT · Infosys · Accenture

What will be the output of the following pseudocode?

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

Correct answer: A. 8

6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 is eight steps.

More Pseudocode and Code Tracing questions

Exams that ask this topic