ProgrammingModerateInfosys · Capgemini pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0
for (i = 1 to 100)
    if ((i mod 3 == 0) OR (i mod 5 == 0))
        if (NOT (i mod 15 == 0))
            c = c + 1
        end if
    end if
end for
Print c
  1. A. 47
  2. B. 53
  3. C. 39
  4. D. 41

Correct answer: D. 41

Multiples of 3 or 5 up to 100: 33 + 20 − 6 = 47. Excluding the 6 multiples of 15 (which are counted in both) leaves 41 numbers divisible by exactly one of 3 and 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic