ProgrammingHardInfosys · Capgemini pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 5, s = 0
for (i = 1 to n)
    for (j = 1 to i)
        if ((i + j) mod 2 == 0)
            s = s + i * j
        end if
    end for
end for
Print s
  1. A. 55
  2. B. 225
  3. C. 70
  4. D. 86

Correct answer: D. 86

Only pairs with i + j even (same parity) count: i = 1: 1; i = 2: 4; i = 3: 3 + 9 = 12; i = 4: 8 + 16 = 24; i = 5: 5 + 15 + 25 = 45. Total 1 + 4 + 12 + 24 + 45 = 86.

More Pseudocode and Code Tracing questions

Exams that ask this topic