ProgrammingVery hardCompetitive coding (CP)

How many steps does this Collatz loop take to reach 1 starting from 27?

How many steps does this Collatz loop take to reach 1 starting from 27?
#include <stdio.h>
int main(void) {
    unsigned n = 27;
    int steps = 0;
    while (n != 1) {
        n = (n % 2) ? 3 * n + 1 : n / 2;
        steps++;
    }
    printf("%d", steps);
}
  1. A. 110
  2. B. 112
  3. C. 111
  4. D. 96

Correct answer: C. 111

The loop applies the odd and even rules until n reaches 1, which takes 111 steps for 27. The peak value on the way is 9232, which does not change the step count.

More C Programming questions

Exams that ask this topic