Finding trailing zeros of a factorial JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

Given an integer n, we have to write a function that returns the number of trailing zeroes in n!.

For example −

trailingZeroes(4) = 0
trailingZeroes(5) = 1
because 5! = 120
trailingZeroes(6) = 1

Example

const num = 17;
const findTrailingZeroes = num => {
   let cur = 5, total = 0;
   while (cur <= num) {
      total += Math.floor(num / cur);
      cur *= 5;
   };
   return total;
};
console.log(findTrailingZeroes(num));
console.log(findTrailingZeroes(5));
 console.log(findTrailingZeroes(1));

Output

And the output in the console will be −

3
1
0
raja
Published on 23-Nov-2020 07:06:39
Advertisements