Multiples of three or four — Pseudocode Aptitude Question
Multiples of three or four: What is printed? Answer and worked solution on CodeKairo.
- Topic: Pseudocode
- Section: Programming MCQs
- Difficulty: Hard
- Time target: 120 seconds
Question
What is printed?
Integer i, c
Set c = 0
For i = 1 to 60
If i MOD 3 == 0 OR i MOD 4 == 0 Then
Set c = c + 1
End If
End For
Print cOptions
- 25
- 30
- 35
- 40
Show the answer and the worked solution
Correct answer: B. 30
Worked solution
- Multiples of 3 up to 60: 60 / 3 = 20 of them.
- Multiples of 4 up to 60: 60 / 4 = 15 of them.
- Numbers counted twice are the multiples of 12: 60 / 12 = 5 of them.
- The OR admits each qualifying number exactly once, so c = 20 + 15 - 5 = 30.
Answer: 30.
The idea
An OR inside a counting loop is set union, so inclusion-exclusion applies: add the two counts and subtract the overlap, which is governed by the LCM. Adding 20 and 15 blindly gives 35, the trap.
Shortcut. The pattern repeats every 12: 1 to 12 holds 3, 4, 6, 8, 9 and 12, six hits, and 60 is five such blocks, so 6 × 5 = 30.
Common trap. 35 adds the 20 multiples of 3 to the 15 multiples of 4 and counts 12, 24, 36, 48 and 60 twice, although the OR moves the counter only once for each.