ProgrammingHardTCS NQT · Cognizant GenC Next

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer count = 0, s
for (each n from 2 to 500)
    s = 1
    for (each d from 2 to n - 1)
        if (n mod d == 0)
            s = s + d
        end if
    end for
    if (s == n)
        count = count + 1
    end if
end for
Print count
  1. A. 2
  2. B. 3
  3. C. 4
  4. D. 1

Correct answer: B. 3

s is the sum of the proper divisors of n (1 included), so the loop counts perfect numbers up to 500: 6, 28 and 496. Output 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic