ProgrammingHardWipro · Capgemini pattern

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 n = 6, steps = 0, peak = n
while (n != 1)
    if (n mod 2 == 0)
        n = n / 2
    else
        n = 3 * n + 1
    end if
    if (n > peak)
        peak = n
    end if
    steps = steps + 1
end while
Print steps, peak
  1. A. 9 16
  2. B. 8 16
  3. C. 8 10
  4. D. 7 16

Correct answer: B. 8 16

6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1: eight steps, and the largest value reached is 16. '9' counts the starting value as a step; '10' stops tracking the peak too early.

More Pseudocode and Code Tracing questions

Exams that ask this topic