ProgrammingHardTCS NQT · Infosys

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0
for (each i from 1 to 10)
    for (each j from 1 to i)
        if (j * j > i)
            break
        end if
        c = c + 1
    end for
end for
Print c
  1. A. 55
  2. B. 22
  3. C. 19
  4. D. 10

Correct answer: C. 19

For each i the inner loop counts j with j² ≤ i, i.e. ⌊√i⌋: 1, 1, 1, 2, 2, 2, 2, 2, 3, 3 for i = 1 to 10. Total 19.

More Pseudocode and Code Tracing questions

Exams that ask this topic