ProgrammingVery hardCAT · MBA placement

The number of trailing zeros of 1000! equals the power of 5 in 1000!, which is the sum of floor(1000 / 5^k) for k = 1, 2, 3, ... The procedure below computes it. What is printed?

The number of trailing zeros of 1000! equals the power of 5 in 1000!, which is the sum of floor(1000 / 5^k) for k = 1, 2, 3, ... The procedure below computes it. What is printed?
count = 0; p = 5
while p <= 1000
    count = count + 1000 div p
    p = p * 5
end while
Print count
  1. A. 397
  2. B. 1098
  3. C. 200
  4. D. 249

Correct answer: D. 249

The factor 5 appears 200 + 40 + 8 + 1 = 249 times, which is the trailing zero count of 1000!. Counting factors of 2 instead gives a much larger number, adding 5 to p instead of multiplying gives 1098, and stopping after the first power gives 200.

More Pseudocode and Code Tracing questions

Exams that ask this topic