ProgrammingModerateGATE CS · TCS NQT

What is printed?

What is printed?
int c = 0;
for (int i = 1; i <= 16; i *= 2)
    for (int j = 0; j < i; j++) c++;
printf("%d", c);
  1. A. 16
  2. B. 80
  3. C. 32
  4. D. 31

Correct answer: D. 31

i takes 1, 2, 4, 8, 16 and the inner loop runs i times: 1 + 2 + 4 + 8 + 16 = 31. A doubling outer loop gives about 2n inner steps in total, not n log n; '80' is 16 × 5.

More C Programming questions

Exams that ask this topic