ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer total = 0
for (each i from 1 to 3)
    for (each j from 1 to 3)
        if (i <= j)
            total = total + i * j
        end if
    end for
end for
Print total
  1. A. 25
  2. B. 36
  3. C. 14
  4. D. 20

Correct answer: A. 25

Only pairs with i<=j contribute: (1,1)=1,(1,2)=2,(1,3)=3,(2,2)=4,(2,3)=6,(3,3)=9. Summing: 1+2+3+4+6+9=25.

More Pseudocode and Code Tracing questions

Exams that ask this topic