ProgrammingModerateAccenture · Cognizant pattern

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Integer n = 100, 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. 17
  2. B. 25
  3. C. 26
  4. D. 24

Correct answer: B. 25

This is the Collatz sequence: 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. That is 25 steps, and c counts steps, not the 26 values.

More Pseudocode and Code Tracing questions

Exams that ask this topic