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)
Function gcd(Integer a, Integer b)
    while (b != 0)
        Integer t = a mod b
        a = b
        b = t
    end while
    return a
End Function
Integer L = 1
for (each i from 1 to 10)
    L = L / gcd(L, i) * i
end for
Print L
  1. A. 3628800
  2. B. 2520
  3. C. 5040
  4. D. 1260

Correct answer: B. 2520

L / gcd(L, i) × i is lcm(L, i), so L becomes lcm(1, …, 10) = 2³ × 3² × 5 × 7 = 2520. 3628800 = 10! multiplies without dividing.

More Pseudocode and Code Tracing questions

Exams that ask this topic