ProgrammingHard

How many times does cnt++ run in: for (i = 0; i < 10; i++) for (j = i; j < 10; j++) cnt++; ?

  1. A. 55
  2. B. 45
  3. C. 100
  4. D. 50

Correct answer: A. 55

The inner loop runs 10 + 9 + ... + 1 times, which is 10 x 11 / 2 = 55. Starting j at i halves the work compared with the full square.

More C Programming questions

Exams that ask this topic