ProgrammingHardTCS NQT · Accenture 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 1 to n)
    for (each j from 1 to 2 * i - 1)
        c = c + 1
    end for
end for
Print c
  1. A. 15
  2. B. 20
  3. C. 25
  4. D. 30

Correct answer: C. 25

For each i, the inner loop runs 2i-1 times: 1,3,5,7,9 for i=1..5. Summing these odd numbers gives 1+3+5+7+9 = 25 (the sum of the first n odd numbers equals n^2 = 25).

More Pseudocode and Code Tracing questions

Exams that ask this topic