ProgrammingHardAccenture · Cognizant

What is printed?

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

Correct answer: C. 8

For each i the inner loop visits the multiples of i up to 4: 4 for i = 1, 2 for i = 2 (2, 4), and 1 each for i = 3 and i = 4. Total 8. '10' counts j from i to 4 in steps of 1.

More C Programming questions

Exams that ask this topic