ProgrammingHardTCS NQT · Infosys · Accenture

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0
for (each n from 2 to 29)
    Boolean prime = true
    for (each d from 2 while d * d <= n)
        if (n mod d == 0) prime = false
    end for
    if (prime) c = c + 1
end for
Print c
  1. A. 10
  2. B. 9
  3. C. 11
  4. D. 8

Correct answer: A. 10

The primes below 30 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 - ten of them.

More Pseudocode and Code Tracing questions

Exams that ask this topic