ProgrammingHardTCS NQT · Infosys · Accenture

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 5)
    for (each j from 1 to 5)
        if ((i + j) mod 3 == 0)
            c = c + 1
        end if
    end for
end for
Print c
  1. A. 7
  2. B. 9
  3. C. 8
  4. D. 10

Correct answer: B. 9

Pairs with i + j = 3, 6 or 9: sum 3 gives 2 pairs, sum 6 gives 5 pairs, sum 9 gives 2 pairs, total 9.

More Pseudocode and Code Tracing questions

Exams that ask this topic