ProgrammingModerateTCS NQT · Wipro

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer s = 0
for (each i from 1 to 10)
    s = s + 100 / i
end for
Print s
  1. A. 291
  2. B. 293
  3. C. 292
  4. D. 281

Correct answer: A. 291

Integer division drops each fraction: 100 + 50 + 33 + 25 + 20 + 16 + 14 + 12 + 11 + 10 = 291. Rounding each quotient instead gives 293, truncating the exact sum 292.9 once gives 292, and stopping at i = 9 gives 281.

More Pseudocode and Code Tracing questions

Exams that ask this topic