JavaScript Program to find Factorial of number using Recursion
The factorial of any positive integer is the product of all positive integers less than or equal to that number. Consider the factorial of 5, which is written as 5! and is equal to 54321 = 120. Counting and probability are just two examples of the many areas of mathematics and computation where this idea is put to use.
Here, we'll study a JavaScript implementation of the recursion technique for computing a number's factorial. Recursion is a useful technique for solving difficult problems by partitioning them into smaller, more manageable chunks. A clear and concise algorithm for computing the factorial of a number can be written using recursion.
In this tutorial, I'll show you how to calculate the factorial of a number using recursion in JavaScript. Everything you need to know to write your own factorial calculator in JavaScript will be covered, from defining the recursive function and setting the base case to testing the programme and getting the final output. Let's jump in and begin computing factorials right away!
We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.
What is Factorial of a Number?
A number's factorial is the sum of all positive integers that are less than or equal to that number. For example, the factorial of 5 (which is written as 5!) is 5 4 3 2 1 = 120. If n is a positive number, you can find its factorial by multiplying all the positive numbers that are less than or equal to n. The definition of the factorial of 0 is 1. Factorials are used in many math and computer problems, like counting and figuring out the odds.
Factorial of n is given as n! = n * (n-1) * ... * 3 * 2 * 1
Note:
- The Factorial of 0 is said to be 1.
- The Factorial of negative numbers does not exist.
Approach
The user is asked to enter a number for which he/she wants the factorial. Upon entering a positive number or 0, the function factorial(num)
gets called.
- If the user enters the number 0, the program will return 1.
- If the user enters a number greater than 0, the program will recursively call itself by decreasing the number one after the other (decreasing by 1).
- This process continues until the number becomes 1. Then when the number reaches 0, 1 is returned. This last condition is called the Base Condition.
Program to find Factorial of number using Recursion
// JavaScript Program to find the factorial of a number
function factorial(x) {
// Base Condition
if (x == 0)
return 1;
else
return x * factorial(x - 1);
}
var num = prompt("Enter a positive number: ");
if (num >= 0) {
const res = factorial(num);
console.log("The factorial of the entered number is "+ result);
}
else
console.log("Enter a positive number.");
Enter a positive number: 6
The factorial of the entered number is 720
Conclusion
In conclusion, the ability to compute the factorial of a number is a fundamental mathematical concept with many real-world applications. Programming the factorial of a number using JavaScript recursion allows for an elegant solution.
Through this guide, you should be able to write a JavaScript program that uses recursion to calculate the factorial of a given number. We've discussed the recursive function's implementation, the base case to terminate the recursion, and the program's output. You should now be well-equipped to use recursion in JavaScript to compute the factorial of a number.
Learning to use recursion effectively in programming is akin to unlocking a secret door that leads to simple solutions to otherwise intractable problems. With any luck, you've learned a thing or two about recursion and JavaScript's factorial function from this article. Thus, maintain your dedication to learning more about JavaScript and honing your skills.