ProgrammingHardTCS NQT · Accenture

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 5, spaces = 0, stars = 0
for (each i from 1 to n)
    for (each j from 1 to n - i) spaces = spaces + 1
    for (each k from 1 to 2 * i - 1) stars = stars + 1
end for
Print spaces, stars
  1. A. 10 25
  2. B. 15 25
  3. C. 10 15
  4. D. 10 30

Correct answer: A. 10 25

This is the pyramid pattern. Spaces: 4 + 3 + 2 + 1 + 0 = 10. Stars: 1 + 3 + 5 + 7 + 9 = 25 (the sum of the first 5 odd numbers is 5²). Output 10 25.

More Pseudocode and Code Tracing questions

Exams that ask this topic