ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The procedure below computes the value of the sum 1^2 + 2^2 + ... + 10^2 using the formula n(n+1)(2n+1)/6 for n = 10, and then the loop sums the squares directly. What is printed by the loop version?

The procedure below computes the value of the sum 1^2 + 2^2 + ... + 10^2 using the formula n(n+1)(2n+1)/6 for n = 10, and then the loop sums the squares directly. What is printed by the loop version?
total = 0
for i = 1 to 10
    total = total + i * i
end for
Print total
  1. A. 285
  2. B. 440
  3. C. 55
  4. D. 385

Correct answer: D. 385

Adding the squares 1 through 100 gives 385, which matches n(n+1)(2n+1)/6. Stopping at 9 gives 285, using i*(i+1) gives 440, and printing the triangular number 55 answers a different sum.

More Pseudocode and Code Tracing questions

Exams that ask this topic