Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Java Program to find Factorial of a Number

Posted in Java Tutorial
Updated on Oct 30, 2024
By Mari Selvan
πŸ‘οΈ 117 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
Java Program to find Factorial of a Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the world of programming, solving mathematical problems is a fundamental skill. One such mathematical operation is finding the factorial of a number.

The factorial of a non-negative integer is the product of all positive integers less than or equal to that number. It's denoted by the symbol !.

In this tutorial, we'll explore a Java program that efficiently calculates the factorial of a given number.

πŸ“„ Example

Let's take a look at the Java code that achieves this functionality.

Factorial.java
Copied
Copy To Clipboard
public class Factorial {

// Function to calculate factorial
static long calculateFactorial(int num) {
  if (num == 0 || num == 1) {
    return 1;
  } else {
    return num * calculateFactorial(num - 1);
  }
}

// Driver program
public static void main(String[] args) {
  // Replace this value with your desired number
  int number = 5;

  // Call the function to calculate factorial
  long result = calculateFactorial(number);

  // Display the result
  System.out.println("Factorial of " + number + " is: " + result);
}
}

πŸ’» Testing the Program

To test the program with a different number, replace the value of number in the main method.

Output
Factorial of 5 is: 120

Compile and run the program to see the factorial in action.

🧠 How the Program Works

  1. The program defines a class Factorial containing a static method calculateFactorial that recursively calculates the factorial of a given number.
  2. In the main method, replace the value of number with the desired input.
  3. The program then calls the calculateFactorial method to compute the factorial.
  4. The result is displayed using System.out.println.

🧐 Understanding the Concept of Factorial

Before delving into the code, let's take a moment to understand the concept of factorial.

The factorial of a non-negative integer n, denoted as n!, is the product of all positive integers less than or equal to n.

For example, the factorial of 5 (5!) is calculated as 5 × 4 × 3 × 2 × 1 = 120.

🎒 Optimizing the Program

While the provided program is effective, it uses recursion to calculate the factorial. Depending on the input value, this approach may lead to a stack overflow for large numbers. Consider exploring iterative solutions for larger inputs or optimizing the recursive approach.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (Java Program to find Factorial of a Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy