ProgrammingVery hardCAT · MBA placement

For N = 100 the procedure below counts how many times 5 divides the factorials, that is, the number of trailing zeros of 100!. The count is the sum of floor(N / 5^k) for k = 1, 2, 3, ... What is printed?

For N = 100 the procedure below counts how many times 5 divides the factorials, that is, the number of trailing zeros of 100!. The count is the sum of floor(N / 5^k) for k = 1, 2, 3, ... What is printed?
count = 0; p = 5
while p <= N
    count = count + N div p
    p = p * 5
end while
Print count
  1. A. 97
  2. B. 66
  3. C. 24
  4. D. 20

Correct answer: C. 24

The factor 5 appears 20 + 4 = 24 times in the product 1 x 2 x ... x 100, and that is the trailing zero count. Counting factors of 2 gives 97, adding 5 instead of multiplying by 5 counts 5s in a different way, and stopping after the first power gives 20.

More Pseudocode and Code Tracing questions

Exams that ask this topic