ProgrammingHardTCS NQT · Accenture

Count the integers n with 1 <= n <= 500 that are divisible by 4 or by 6. The procedure below uses a single loop. What is printed?

Count the integers n with 1 <= n <= 500 that are divisible by 4 or by 6. The procedure below uses a single loop. What is printed?
count = 0
for n = 1 to 500
    if n mod 4 == 0 or n mod 6 == 0 then count = count + 1
end for
Print count
  1. A. 125
  2. B. 167
  3. C. 83
  4. D. 41

Correct answer: B. 167

Multiples of 4 number 125 and multiples of 6 number 83, with 41 multiples of 12 counted in both, so the union is 125 + 83 - 41 = 167. Counting only one of the two divisors gives 125 or 83, and using and gives only the multiples of 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic