ProgrammingModerateTCS NQT · Wipro

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 49)
    if (i mod 3 == 0 OR i mod 5 == 0)
        s = s + i
    end if
end for
Print s
  1. A. 543
  2. B. 651
  3. C. 618
  4. D. 593

Correct answer: A. 543

Multiples of 3 below 50 add to 3 × (1 + … + 16) = 408, multiples of 5 to 5 × (1 + … + 9) = 225, and multiples of 15 (counted twice) to 15 + 30 + 45 = 90. 408 + 225 − 90 = 543.

More Pseudocode and Code Tracing questions

Exams that ask this topic