ProgrammingHardAccenture · Cognizant

What is printed?

What is printed?
int i, s = 0;
for (i = 1; i < 50; i = i * 2 + 1)
    s += i % 3 ? i : -i;
printf("%d %d", s, i);
  1. A. 21 63
  2. B. 21 32
  3. C. 57 63
  4. D. 21 31

Correct answer: A. 21 63

i runs 1, 3, 7, 15, 31 and then becomes 63, which fails the test. Multiples of 3 are subtracted: 1 − 3 + 7 − 15 + 31 = 21. After the loop i holds 63, the value that failed; '31' is the last value used inside the loop.

More C Programming questions

Exams that ask this topic