ProgrammingModerateWipro · Capgemini pattern

What is printed?

What is printed?
int i, j, s = 0;
for (i = 0, j = 10; i < j; i += 2, j--)
    s += j - i;
printf("%d %d %d", i, j, s);
  1. A. 6 7 22
  2. B. 8 6 18
  3. C. 8 6 22
  4. D. 8 7 22

Correct answer: C. 8 6 22

Pairs (i, j): (0, 10) adds 10, (2, 9) adds 7, (4, 8) adds 4, (6, 7) adds 1 → 22. Then i = 8, j = 6 and the condition fails. The comma operator lets the for loop update two variables.

More C Programming questions

Exams that ask this topic