ProgrammingVery hardAmazon OA · 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 = 36, s = 0
for (each i from 1 to n)
    if (i * i > n)
        break
    end if
    if (n mod i == 0)
        s = s + i + n / i
    end if
end for
Print s
  1. A. 91
  2. B. 97
  3. C. 85
  4. D. 55

Correct answer: B. 97

Pairing divisors i and n / i up to √n is fast, but for i = 6 both members of the pair are 6, so it is added twice. The true divisor sum of 36 is 91; the code prints 97.

More Pseudocode and Code Tracing questions

Exams that ask this topic