Open In App

GATE | GATE-CS-2007 | Question 85

Consider the following segment of C-code:

  int j, n;
  j = 1;
  while (j 

The number of comparisons made in the execution of the loop for any n > 0 is:



Base of Log is 2 in all options.
(A) CEIL(logn) + 2
(B) n
(C) CEIL(logn)
(D) FLOOR(logn) + 2

Answer: (D)
Explanation:

We can see it by taking few examples like n = 1, n = 3, etc.

For example, for n=5 we have the following (4) comparisons:
------------------------
1 

Quiz of this Question



Article Tags :