ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode (counting trailing zeros in n factorial, via counting factors of 5)? (All variables are integers, so / is integer division.)

What will be the output of the following pseudocode (counting trailing zeros in n factorial, via counting factors of 5)? (All variables are integers, so / is integer division.)
Function trailingZeros(Integer n)
    Integer c = 0, p = 5
    while (p <= n)
        c = c + n / p
        p = p * 5
    end while
    return c
End Function

Print trailingZeros(100)
  1. A. 20
  2. B. 24
  3. C. 25
  4. D. 19

Correct answer: B. 24

Counting multiples of 5, 25, 125,... up to 100: 100/5=20, 100/25=4, 100/125=0 (loop stops since 125>100). Total 20+4 = 24 trailing zeros in 100!.

More Pseudocode and Code Tracing questions

Exams that ask this topic