Data Structures & AlgorithmsModerateTCS NQT · number theory

Problem: How many integers in [1, 100] are divisible by both 3 and 5? Run the code.

Problem: How many integers in [1, 100] are divisible by both 3 and 5? Run the code.
print(sum(1 for x in range(1, 101) if x % 3 == 0 and x % 5 == 0))
What is printed?
  1. A. 5
  2. B. 7
  3. C. 6
  4. D. 33

Correct answer: C. 6

Being divisible by both 3 and 5 means divisible by 15, giving 15, 30, 45, 60, 75 and 90, which is 6 numbers. Checking only divisibility by 3 gives the trap 33.

More Coding Interview Problems questions

Exams that ask this topic