ProgrammingVery hardAccenture · TCS · Infosys

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0
for (each i from 1 to 10)
    for (each j from i + 1 to 10)
        if ((i + j) mod 3 == 0)
            c = c + 1
        end if
    end for
end for
Print c
  1. A. 14
  2. B. 15
  3. C. 18
  4. D. 16

Correct answer: B. 15

The inner loop runs over strictly j > i (unordered pairs), counting pairs whose sum is a multiple of 3. Tracing i from 1 to 10 gives exactly 15 such pairs. Starting j at i instead of i+1 (also counting i+i) gives the trap 18.

More Pseudocode and Code Tracing questions

Exams that ask this topic