ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0, n = 5
for (each i from n down to 1)
    for (each j from 1 to i)
        if (j mod 2 == 0)
            c = c + 1
        end if
    end for
end for
Print c
  1. A. 5
  2. B. 6
  3. C. 8
  4. D. 10

Correct answer: B. 6

Counting even j values for each i: i=5 gives 2 (j=2,4), i=4 gives 2, i=3 gives 1, i=2 gives 1, i=1 gives 0. Total 2+2+1+1+0 = 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic