Data Structures & AlgorithmsModerateGATE CS · competitive programming

For (i = 1; i ≤ n; i *= 3) with n = 1000, how many times does the body run?

  1. A. 7
  2. B. 6
  3. C. 8
  4. D. 333

Correct answer: A. 7

i takes 1, 3, 9, 27, 81, 243, 729 (the next value 2187 exceeds 1000), giving 7 iterations, which is ⌊log₃ 1000⌋ + 1.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic