ProgrammingHardTCS NQT Advanced · Infosys SP

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer n = 360, d = 2, s = 0, c = 0
while (n > 1)
    while (n mod d == 0)
        s = s + d
        c = c + 1
        n = n / d
    end while
    d = d + 1
end while
Print s, c
  1. A. 17 6
  2. B. 10 3
  3. C. 17 3
  4. D. 15 6

Correct answer: A. 17 6

The loops strip out prime factors with repetition: 360 = 2 × 2 × 2 × 3 × 3 × 5. Their sum is 17 and there are 6 of them: 17 6. 10 3 adds only the distinct primes.

More Pseudocode and Code Tracing questions

Exams that ask this topic