ProgrammingHardTCS NQT · Infosys · Capgemini

What will be the output of the following pseudocode? (break leaves only the innermost loop; continue skips to the next iteration of the innermost loop.)

What will be the output of the following pseudocode? (break leaves only the innermost loop; continue skips to the next iteration of the innermost loop.)
Integer s = 0
For i = 1 to 6
    For j = i to 6
        If (i + j) mod 3 = 0 Then Continue
        If i * j > 20 Then Break
        s = s + i * j
    End For
End For
Print s
  1. A. 157
  2. B. 151
  3. C. 89
  4. D. 27

Correct answer: C. 89

Each inner loop skips pairs whose sum is a multiple of 3 and abandons the row once i*j exceeds 20, but the outer loop still moves to the next i. Adding the surviving products row by row gives the printed total.

More Pseudocode and Code Tracing questions

Exams that ask this topic