ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The loops below print the pairs (i, j) with 1 <= i <= j <= 3 in order, and the procedure prints the total count of pairs. What is printed?

The loops below print the pairs (i, j) with 1 <= i <= j <= 3 in order, and the procedure prints the total count of pairs. What is printed?
count = 0
for i = 1 to 3
    for j = i to 3
        count = count + 1
    end for
end for
Print count
  1. A. 3
  2. B. 9
  3. C. 6
  4. D. 10

Correct answer: C. 6

The pairs are (1,1), (1,2), (1,3), (2,2), (2,3) and (3,3), which is 6. Requiring i < j gives 3, using all j values gives 9, and adding i per pass gives 14.

More Pseudocode and Code Tracing questions

Exams that ask this topic