Examples
- JavaScript Program to Check if a Number is Positive, Negative, or Zero
- JavaScript Program to Find the Largest Among Three Numbers
- JavaScript Program to Check Prime Number (4 Ways)
- JavaScript Program to Find the Factorial of a Number
- Armstrong Number in JavaScript (6 Programs)
- JavaScript Program to Find HCF or GCD
- JavaScript Program to Find LCM (5 Ways)
- JavaScript Program to Convert Decimal to Binary
JavaScript Program to Find the Factorial of a Number
Factorial is a mathematical operation that multiplies a number by all the positive integers less than itself. Represented as n!, it is a key concept in mathematics and is extensively used in combinatorics, algebra, probability, computer science, and algorithmic problem-solving. Factorial calculations are particularly important in understanding permutations, combinations, and various mathematical proofs. For example:
-
5! = 5 × 4 × 3 × 2 × 1 = 120
-
0! = 1 (special case by convention)
This tutorial explores multiple JavaScript methods to compute the factorial of a number.
Factorial Program in JavaScript Using for Loop
The simplest way to calculate the factorial of a number is by using a for loop.
Code
function factorialUsingForLoop(number) {
if (number < 0) {
return "Factorial of a negative number is not defined.";
}
let factorial = 1;
for (let i = 1; i <= number; i++) {
factorial *= i;
}
return factorial;
}
// Test the function
const num = 5;
console.log(`Factorial of ${num} is ${factorialUsingForLoop(num)}.`);
Output
Factorial of 5 is 120.
Explanation
-
The function initializes factorial to 1.
-
A for loop iterates from 1 to the given number, multiplying factorial by the loop variable i.
-
The result is returned after the loop completes.
This is a simple example of a factorial program in JavaScript using a loop.
JavaScript Program to Check Factorial of Number Using Recursion
A recursive function can be used to calculate the factorial by calling itself.
Code
function factorialUsingRecursion(number) {
if (number < 0) {
return "Factorial of a negative number is not defined.";
}
if (number === 0 || number === 1) {
return 1;
}
return number * factorialUsingRecursion(number - 1);
}
// Test the function
const num2 = 6;
console.log(`Factorial of ${num2} is ${factorialUsingRecursion(num2)}.`);
Output
Factorial of 6 is 720.
Explanation
-
If the number is less than 0, the function returns an error message.
-
If the number is 0 or 1, the function returns 1 (base case).
-
For other numbers, the function multiplies the number by the factorial of the number minus 1 (recursive case).
This demonstrates a JavaScript program to find factorial of a number using recursion, a commonly used approach in problem-solving.
Factorial of a Number in JavaScript Using while Loop
The while loop can also be used to calculate the factorial of a number.
Code
function factorialUsingWhileLoop(number) {
if (number < 0) {
return "Factorial of a negative number is not defined.";
}
let factorial = 1;
let i = 1;
while (i <= number) {
factorial *= i;
i++;
}
return factorial;
}
// Test the function
const num3 = 4;
console.log(`Factorial of ${num3} is ${factorialUsingWhileLoop(num3)}.`);
Output
Factorial of 4 is 24.
Explanation
-
The function initializes factorial to 1 and i to 1.
-
The while loop multiplies factorial by i and increments i until i exceeds the given number.
-
The final result is returned after the loop.
This is another way to calculate the factorial in JavaScript using iteration.
Concepts Used in Above Programs
Iteration
-
Loops like for and while are used to repeat tasks efficiently.
Recursion
-
A function calls itself with smaller inputs until a base case is met.
Conditional Statements
-
if conditions handle special cases like negative numbers and base cases.
Multiplication
-
Used to calculate the factorial iteratively or recursively.
Error Handling
-
Ensures that invalid inputs (like negative numbers) are handled gracefully.