JavaScript Code to Calculate Factorial of any number

Factorial of a number, is the product of all positive integer number less or equal than that given number.

factorialnumber


i.e, Factorial of 5 is 5x4x3x2x1=120 and is denoted by 5!

JavaScript code will take any number by using prompt command and then perform the logic. 

The factorial of negative number doesn't exist and the factorial of zero and one is one.

The factorial of any other positive number is obtained by performing for loop in JavaScript.


The code to find factorial of any positive number is :

<HTML>

<HEAD>

<TITLE> Factorial of any given number</TITLE>

<SCRIPT>

LET n = parseInt(prompt('Enter a positive integer: '));


if (n < 0) {   

    alert("Factorial for negative number does not exist");

                }

else if (n = = 0||n= =1) {

    alert ("The Factorial is 1");

}

else {

    let fact = 1;

    for (i = 1; i <= n; i++) {

        fact *= i;

    }

    alert("The factorial is ${fact}");

}

</SCRIPT>

</HEAD>

<BODY>

</BODY>

</HTML>


Read this also :

How to connect PHP and MYSQL ?

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.