ProgrammingEasyAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer s = 0
for (i = 1 to 5)
    for (j = i to 5)
        s = s + 1
    end for
end for
Print s
  1. A. 25
  2. B. 15
  3. C. 10
  4. D. 20

Correct answer: B. 15

The inner loop runs 5, 4, 3, 2, 1 times: 15 = 5·6/2. Running j from 1 to 5 every time would give 25; starting j at i + 1 would give 10.

More Pseudocode and Code Tracing questions

Exams that ask this topic