JavaScript Program to Find the Factorial of a Number

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Introduction

The factorial of a number is the product of all positive integers less than or equal to that number. It is denoted as n!. For example, the factorial of 5 is 5! = 5 * 4 * 3 * 2 * 1 = 120. Factorials are used in many areas of mathematics, especially in combinatorics, algebra, and calculus. This program helps calculate the factorial of a given number in JavaScript.

Problem Statement

Create a JavaScript program that:

  • Accepts a positive integer.
  • Calculates and returns the factorial of that number.

Example:

  • Input: 5

  • Output: 120

  • Input: 3

  • Output: 6

Solution Steps

  1. Read the Input Number: Provide the number either as user input or directly within the code.
  2. Handle Edge Case: If the input number is 0, return 1 as 0! = 1.
  3. Calculate the Factorial: Use either an iterative loop or recursion to calculate the factorial.
  4. Display the Result: Print the calculated factorial.

JavaScript Program

// JavaScript Program to Find the Factorial of a Number // Author: https://www.javaguides.net/ function factorial(n) { // Step 1: Handle base case if (n === 0 || n === 1) { return 1; } // Step 2: Calculate the factorial iteratively let result = 1; for (let i = 2; i <= n; i++) { result *= i; } return result; } // Example input let inputNumber = 5; let result = factorial(inputNumber); console.log(`The factorial of ${inputNumber} is: ${result}`); 

Output

The factorial of 5 is: 120 

Example with Different Input

let inputNumber = 3; let result = factorial(inputNumber); console.log(`The factorial of ${inputNumber} is: ${result}`); 

Output:

The factorial of 3 is: 6 

Explanation

Step 1: Handle the Base Case

  • If the number is 0 or 1, the factorial is defined as 1, so the program returns 1 immediately for these values.

Step 2: Calculate the Factorial Iteratively

  • For numbers greater than 1, the function uses a for loop to calculate the factorial by multiplying the result variable with each number from 2 to n.

Step 3: Display the Result

  • The calculated factorial is printed using console.log().

Conclusion

This JavaScript program demonstrates how to calculate the factorial of a given number using an iterative approach. Factorials are a fundamental mathematical concept, and this program provides a simple and efficient way to compute them in JavaScript. You can modify this solution to use recursion or handle larger inputs if needed.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare