How to to find the factorial of a number – Javacript ( Only Code)

Spread the love


// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));
// checking if number is negative
if (number < 0) {
console.log('Wrong Input - Factorial for negative number does not exist.');
}
// if number is 0
else if (number === 0) {
console.log(`The factorial of ${number} is 1.`);
}
// if number is positive
else {
let fact = 1;
for (i = 1; i <= number; i++) {
fact *= i;
}
console.log(`The factorial of ${number} is ${fact}.`);
}

If the input is 5,factorial of 5 is equal to 1 * 2 * 3 * 4 * 5 = 120

admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *