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- A. 285
- B. 440
- C. 55
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Count the integers n with 1 <= n <= 1000 that are divisible by 7 but NOT divisible by 11. The procedure below does this with one loop…
- Count the integers n with 1 <= n <= 500 that are divisible by 4 or by 6. The procedure below uses a single loop. What is printed? count =…
- Find the gcd of the array [84, 126, 210, 462] by folding the Euclidean gcd across the elements, starting from 0. The procedure below runs…
- The number of trailing zeros of 1000! equals the power of 5 in 1000!, which is the sum of floor(1000 / 5^k) for k = 1, 2, 3, ... The…
- The least common multiple of the integers 1 through 10 is computed by folding lcm(a, b) = a b div gcd(a, b) from L = 1. The procedure…
- Count the three-digit numbers (100 to 999) in which every digit is odd. The procedure below uses a loop over the numbers and checks each…