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)- A. 20
- B. 24
- C. 25
- 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!.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (fast exponentiation)? (All variables are integers, so / is integer division.) Integer…
- What will be the output of the following pseudocode (checking if one string is a rotation of another)? Function isRotation(String a…
- What will be the output of the following pseudocode (counting total set bits across 1..n)? Function countSetBitsRange(Integer n) Integer…
- What will be the output of the following pseudocode (depth-first traversal of a graph given as an adjacency list)? Map graph = {0: [1, 2]…
- What will be the output of the following pseudocode (breadth-first traversal using a queue)? Map graph = {0: [1, 2], 1: [0, 3], 2: [0, 3]…
- What will be the output of the following pseudocode? Integer x = 1, i = 1 while (i <= 5) x = x + i i i = i + 1 end while Print x