Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Program to Find Factorial of a Number using Recursion

JavaScript Program to Find Factorial of a Number using Recursion

In this article, we are going to learn about finding a Factorial of a Number using Recursion. Calculating the factorial of a number using recursion means implementing a function that calls itself to find the factorial of a given number. The factorial of a non-negative integer ā€œnā€ is the product of all positive integers less than or equal to ā€œnā€.

The factorial of a positive number n is given by:

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

Example:

Input : N = 6 
Output : The factorial of given number is : 720
Explanation: 6 * 5 * 4 * 3 * 2 * 1 = 720
Hence factorial of 6 is : 720

There are some common approaches that can be used to Find Factorial of Number Using Recursion, which is listed below:

  • Factorial using Recursion in JavaScript
  • Using Ternary Operator with Recursive Approach

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Factorial using Recursion in JavaScript

To calculate the factorial of a number ā€œNā€ using recursion, we first establish base cases: if N is 0 or 1, the factorial is 1. Otherwise, we recursively compute N * factorial(N-1) to find the product of all positive integers up to N.

Syntax:

function factorial(n) {
if (n === 0 || n === 1) {
// Code...
}
}

Example: In this example, we are using the above-explained approach.

Javascript




function factorial(n) {
Ā Ā Ā Ā if (n === 0 || n === 1) {
Ā Ā Ā Ā Ā Ā Ā Ā return 1;
Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā return n * factorial(n - 1);
Ā Ā Ā Ā }
}
let num1 = 6;
let result = factorial(num1);
console.log("The factorial of given number is :" + result);


Output

The factorial of given number is :720

Approach 2: Using Ternary Operator with Recursive Approach

In this approach the recursive function uses the ternary operator to check if the base case (num === 0) is met, returning 1, otherwise calls itself with (num ā€“ 1) and multiplying num with the result.

Syntax:

condition ? value if true : value if false

Example: In this example, we are using the above-explained approach.

factorial of 10 is, 10! = 10*9*8*7*6*5*4*3*2*1 = 3628800

Javascript




function factorialFunction(num) {
Ā Ā Ā Ā return num === 0 ? 1 : num * factorialFunction(num - 1);
}
Ā Ā 
const num1 = 10;
const result = factorialFunction(num1);
console.log(result);


Output

3628800
Whether youā€™re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions weā€™ve already empowered, and weā€™re here to do the same for you. Donā€™t miss out ā€“ check it out now!

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?