ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The procedure below counts the integers n with 1 <= n <= 60 that are not divisible by 2, 3 or 5 (coprime to 30). What is printed?

The procedure below counts the integers n with 1 <= n <= 60 that are not divisible by 2, 3 or 5 (coprime to 30). What is printed?
count = 0
for n = 1 to 60
    if n mod 2 != 0 and n mod 3 != 0 and n mod 5 != 0 then count = count + 1
end for
Print count
  1. A. 20
  2. B. 16
  3. C. 58
  4. D. 14

Correct answer: B. 16

The numbers from 1 to 60 coprime to 30 are 1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53 and 59, which is 16. Ignoring the test for 5 gives 20, using or instead of and counts almost every number, and also testing for 7 removes 7 and 49.

More Pseudocode and Code Tracing questions

Exams that ask this topic