ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

A product of ten consecutive numbers 1 x 2 x ... x 10 is computed. The procedure below counts how many of the numbers 1 to 10 are divisible by 2 or by 3. What is printed?

A product of ten consecutive numbers 1 x 2 x ... x 10 is computed. The procedure below counts how many of the numbers 1 to 10 are divisible by 2 or by 3. What is printed?
count = 0
for i = 1 to 10
    if i mod 2 == 0 or i mod 3 == 0 then count = count + 1
end for
Print count
  1. A. 1
  2. B. 14
  3. C. 7
  4. D. 6

Correct answer: C. 7

The numbers 2, 3, 4, 6, 8, 9 and 10 are divisible by 2 or 3, giving 7. Requiring both rules gives 1 (only 6), doubling each count gives 14, and stopping at 9 drops 10 and gives 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic