ProgrammingEasyAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0
for (i = 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. 9
  3. C. 6
  4. D. 4

Correct answer: B. 9

Multiples of 3 up to 20: 3, 6, 9, 12, 15, 18 (6). Multiples of 5: 5, 10, 15, 20 (4). 15 is in both and is counted once by 'or': 6 + 4 − 1 = 9. Adding without removing the overlap gives 10.

More Pseudocode and Code Tracing questions

Exams that ask this topic