ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The procedure below counts the integers from 1 to 29 that are divisible by 3 or by 5. What is printed?

The procedure below counts the integers from 1 to 29 that are divisible by 3 or by 5. What is printed?
count = 0
for n = 1 to 29
    if n mod 3 == 0 or n mod 5 == 0 then count = count + 1
end for
Print count
  1. A. 13
  2. B. 14
  3. C. 9
  4. D. 5

Correct answer: A. 13

Multiples of 3 below 30 are 9 numbers, multiples of 5 are 5 numbers, and 15 is counted in both, so 9 + 5 - 1 = 13. Including 30 gives 14, the multiples of 3 alone give 9, and the multiples of 5 alone give 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic