ProgrammingVery hardGATE CS

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int i, j, k = 0;
    for (i = 0, j = 20; i < j; i += 3, j -= 2)
        k += j - i;
    printf("%d %d %d", i, j, k);
    return 0;
}
  1. A. 12 12 50
  2. B. 9 14 50
  3. C. 12 12 52
  4. D. 12 10 50

Correct answer: A. 12 12 50

The pairs (i, j) at the start of each iteration are (0,20), (3,18), (6,16), (9,14), each adding j - i = 20, 15, 10, 5, so k = 50. After the fourth update i = 12 and j = 12, and the test 12 < 12 fails.

More C Programming questions

Exams that ask this topic