ProgrammingVery hardGATE CS · Campus placements

Starting at n = 27, the loop while (n != 1) { n = (n % 2) ? 3 * n + 1 : n / 2; steps++; } runs with int n. What is steps at the end?

  1. A. 111
  2. B. 96
  3. C. 112
  4. D. 110

Correct answer: A. 111

The Collatz sequence from 27 takes 111 steps to reach 1, and it climbs as high as 9232 on the way. All values stay well inside int, so the loop is defined.

More C Programming questions

Exams that ask this topic