ProgrammingModerateTCS NQT · Wipro

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 99)
    if (i mod 2 == 1)
        s = s + i
    else
        s = s - i
    end if
end for
Print s
  1. A. 49
  2. B. 50
  3. C. 99
  4. D. 0

Correct answer: B. 50

Pair the terms: (1 − 2) + (3 − 4) + … + (97 − 98) = 49 × (−1) = −49, plus the last term 99: s = 50.

More Pseudocode and Code Tracing questions

Exams that ask this topic