ProgrammingEasyTCS NQT · Accenture pattern

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 20)
    if (i mod 3 == 0 or i mod 5 == 0)
        c = c + 1
    end if
end for
Print c
  1. A. 10
  2. B. 1
  3. C. 9
  4. D. 8

Correct answer: C. 9

There are 6 multiples of 3 and 4 multiples of 5 up to 20, but 15 is in both: 6 + 4 − 1 = 9. Forgetting the overlap gives 10, reading 'or' as 'and' counts only 15 (1), and stopping the loop at 19 drops 20 (8).

More Pseudocode and Code Tracing questions

Exams that ask this topic