Q:

Write a JavaScript program to find the number of trailing zeros in the decimal representation of the factorial of a given number

0

Write a JavaScript program to find the number of trailing zeros in the decimal representation of the factorial of a given number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function trailing_zeros_factorial(n) {
    var result = 0;
    for (var i = 5; i <= n; i += 5) {
        var num = i;
        while (num % 5 === 0) {
            num /= 5;
            result++;
        }
    }
    return result;
}

console.log(trailing_zeros_factorial(8))
console.log(trailing_zeros_factorial(9))
console.log(trailing_zeros_factorial(10))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now