ProgrammingVery hard

A loop does for (i = n; i > 0; i /= 2) for (j = 0; j < i; j++) work();. For n = 1024, how many times is work() called in total?

  1. A. 1024
  2. B. 2048
  3. C. 2047
  4. D. 2046

Correct answer: C. 2047

The outer loop takes i = 1024, 512, ..., 1, and the inner loop runs i times for each. The total is 1024 + 512 + ... + 1 = 2047.

More C Programming questions

Exams that ask this topic