ProgrammingHardCapgemini · Infosys pattern

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 = 100, c = 0, d = 5
while (d <= n)
    c = c + n / d
    d = d * 5
end while
Print c
  1. A. 22
  2. B. 20
  3. C. 24
  4. D. 25

Correct answer: C. 24

This is Legendre's count of the factor 5 in 100!, which is the number of trailing zeros: 100 / 5 + 100 / 25 = 20 + 4 = 24 (125 > 100 stops the loop). Counting only multiples of 5 gives 20.

More Pseudocode and Code Tracing questions

Exams that ask this topic