ProgrammingVery hardGATE CS · Campus placements

int count = 0; for (int i = 1; i <= 10; i++) for (int j = i; j <= 10; j += i) count++; What is count at the end?

  1. A. 10
  2. B. 55
  3. C. 27
  4. D. 45

Correct answer: C. 27

The inner loop runs floor(10 / i) times for each i, giving 10 + 5 + 3 + 2 + 2 + 1 + 1 + 1 + 1 + 1 = 27. This harmonic count is O(n log n), which is why such loops are acceptable in contests.

More C Programming questions

Exams that ask this topic