JavaScript Program to Find the Factorial of a Number

To understand this example, you should have the knowledge of the following JavaScript programming topics:


The factorial of a number is the product of all the numbers from 1 to that number. For example,

factorial of 5 is equal to 1 * 2 * 3 * 4 * 5 = 120.

The factorial of a positive number n is given by:

factorial of n (n!) = 1 * 2 * 3 * 4.....n

The factorial of negative numbers do not exist and the factorial of 0 is 1.


Example: Find Factorial

// program to find the factorial of a number

// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));

// checking if number is negative
if (number < 0) {
    console.log('Error! 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}.`);
}

Output

Enter a positive integer: 5
The factorial of 5 is 120.

In the above program, the user is prompted to enter an integer. Then if...else if...else statement is used to check the condition of a number.

  • When the user enters a negative number, an error message is shown.
  • When the user enters 0, the factorial is 1.
  • When the user enters a positive integer, a for loop is used to iterate over 1 to the number entered by the user to find the factorial.
  • Each number is multiplied and stored in the fact variable.

Before we wrap up, let’s put your knowledge of JavaScript Program to Find the Factorial of a Number to the test! Can you solve the following challenge?

Challenge:

Write a function to calculate the factorial of a number.

  • The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.
  • For example, the factorial of 3 is 3 * 2 * 1 = 6.
  • Return the factorial of the input number num.
Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community