AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Hardfor loopcounting

~120s

Multiples of three or four

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 c
Show the answer and the worked solution

Correct answer: B.

30

Worked solution

  1. Multiples of 3 up to 60: 60 / 3 = 20 of them.
  2. Multiples of 4 up to 60: 60 / 4 = 15 of them.
  3. Numbers counted twice are the multiples of 12: 60 / 12 = 5 of them.
  4. 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.

Multiples of three or four — Pseudocode Aptitude Question

Multiples of three or four: What is printed? Answer and worked solution on CodeKairo.

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 c

Options

  1. 25
  2. 30
  3. 35
  4. 40
Show the answer and the worked solution

Correct answer: B. 30

Worked solution

  1. Multiples of 3 up to 60: 60 / 3 = 20 of them.
  2. Multiples of 4 up to 60: 60 / 4 = 15 of them.
  3. Numbers counted twice are the multiples of 12: 60 / 12 = 5 of them.
  4. 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.

All Pseudocode questions · placement mock tests