ProgrammingHardTCS NQT · Wipro

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer count = 0, n = 2, s = 0
while (count < 10)
    Boolean pr = true
    for (each d from 2 to n - 1)
        if (n mod d == 0) pr = false
    end for
    if (pr)
        s = s + n
        count = count + 1
    end if
    n = n + 1
end while
Print s, n
  1. A. 129 29
  2. B. 129 30
  3. C. 100 30
  4. D. 158 31

Correct answer: B. 129 30

The first 10 primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, with sum 129. After 29 is counted, n is still increased once more, so n = 30: 129 30.

More Pseudocode and Code Tracing questions

Exams that ask this topic