ProgrammingHardTCS Digital · Infosys SP

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

Correct answer: A. 60

The loops visit every set of three different numbers from 1 to 10 (C(10, 3) = 120). An even sum needs three evens (C(5, 3) = 10) or one even and two odds (5 × C(5, 2) = 50): c = 60.

More Pseudocode and Code Tracing questions

Exams that ask this topic