ProgrammingHardSSC CGL Tier 2 · IBPS PO Mains

For i from 0 to 30 inclusive, count the values of i with ii <= 30. What is printed?

For i from 0 to 30 inclusive, count the values of i with i*i <= 30. What is printed?
count = 0
for i = 0 to 30
    if i * i <= 30 then count = count + 1
end for
Print count
  1. A. 31
  2. B. 7
  3. C. 5
  4. D. 6

Correct answer: D. 6

The squares 0, 1, 4, 9, 16 and 25 are at most 30, so six values of i count, including i = 0. Dropping i = 0 gives 5, comparing against 36 adds a value, and comparing i with 30 instead of i squared counts every value.

More Pseudocode and Code Tracing questions

Exams that ask this topic