ProgrammingVery hardCAT · MBA placement

The procedure below sums the integers from 1 to 100 that are NOT divisible by 2 and NOT divisible by 3. What is printed?

The procedure below sums the integers from 1 to 100 that are NOT divisible by 2 and NOT divisible by 3. What is printed?
total = 0
for n = 1 to 100
    if n mod 2 != 0 and n mod 3 != 0 then total = total + n
end for
Print total
  1. A. 1633
  2. B. 3417
  3. C. 33
  4. D. 4234

Correct answer: A. 1633

The numbers not divisible by 2 or 3 are 1, 5, 7, 11, 13 and so on, and their sum is 1633. Summing the excluded numbers gives 3417, counting instead of summing gives 33, and using or instead of and gives 4234.

More Pseudocode and Code Tracing questions

Exams that ask this topic