# Factorial program in Java

This article will teach you how to find factorial using JAVA language. Factorial is calculated using recursion and loops like for and while loop.

Factorial is considered an essential function in mathematics used to calculate probability, algebra, power series and many more in maths. The exclamation symbol denotes it “!”. It is important and necessary to know about this basic function of mathematics to make mathematics easy to grasp.

As with other programming languages like c and C++, making the factorial program in Java is quite easy. Things we are going to learn in this article are given below.

**What is factorial in math?****Why is factorial used?****How to find the factorial of any number in math?****Program to calculate the factorial of a number in Java using for loop****Program to calculate the factorial of a number in Java using for loop with taking the value from the user****Program to calculate the factorial of a number in Java using a while loop****Program to calculate the factorial of a number in Java using recursion****Program to calculate the factorial of a number in Java using methods**

In this article, we will dive deep into the factorial and its program in Java. The simple way to make a Java program for finding the factorial of a number is to use a loop like for and while loop in **Java**. Let’s proceed further to have a deep understanding of this area.

**What is factorial in math?**

In mathematics, the factorial of any number is the multiplication of that number by each number below it till it reaches the last number, which is 1. Factorials are used in probability theory to calculate the number of probable outcomes of an event or arrangement. One can see the use of factorial in various mathematical theorems like binomial and Taylor series.

It is also used in combinatorics, by which one can compute the number of orders in which something can occur. Many software programs and algorithms are designed to calculate factorial as it can quickly become large.

*Explore Java Courses*

*Explore popular free Java Courses*

**Why is factorial used?**

Let’s take an example of books to get a hand on this topic. Suppose you have 4 books and want to know how many ways you can arrange them on a bookshelf, and in such a situation, factorial is used. So the number of ways to arrange the books on a bookshelf will be 4!

By calculating it: 4!= 4x3x2x1= 24

This means there are 24 ways to arrange the books on the bookshelf. In the same way, one can arrange three books in 6 different ways by calculating the factorial of 3. And by using factorial, one can easily calculate the probable outcomes of an event or arrangement.

**How to find the factorial of any number in math?**

In math, to calculate the factorial of any number, one has to multiply that number by each number below it until it reaches the last number, 1.

**Formula to calculate the factorial of any number**

n! = n × (n-1) × (n-2) × (n-3) × ….× 3 × 2 × 1

Where “!” is the symbol that denotes the factorial of any number n.

For example, if one wishes to find the factorial to number 4, then its factorial will be:

4!= 4x3x2x1=24

Factorial of 5

5!= 5x4x3x2x1=120

Factorial of 3

3!=3x2x1=6

And this is how in the above example, the factorial of each number is the product of each number to the number itself till the 1—being concerned with the above example 3! It is the product of numbers 3, 2 and 1. Now we know that we can find the factorial of any number by making a Java program.

This article mentions different ways to write a factorial program in Java.

**Program to calculate the factorial of a number in Java using for loop**

//creating a class named Exampleoffactorialclass Exampleoffactorial{ public static void main(String args[]){ //main method int a=6; //initialising and declaring a variable to calculate its factorial int n,factorial=1; //declaring another variable to be used in for loop //for loop to calculate the factorial of 6 for(n=1;n<=a;n++){ factorial=factorial*n; } System.out.println("so the factorial of"+a+" is "+factorial); } //message to be displayed on the screen }

**Output1**

So the factorial of 6 is 720

**Explanation**

- As we know about the logic behind the factorial program in Java
- In for loop, the value of a is fixed as 6 and the value of n is initialised as 1, which is compared to the value of a.
- Each iteration factorial variable stores the product of previous numbers with the incremented number.
- In each iteration, the value of n is increased by one using the increment operator ++.
- This is how for loop is used to make the factorial program in Java.

**Program to calculate the factorial of a number in Java using for loop with taking the value from the user**

//creating a class named ExampleofFactorialclass ExampleofFactorial{ public static void main(String arg[]){ //main method long a; //initialising variable factorial=1; //initialising and declaring the variable Scanner sc=new Scanner(System.in); //creating the scanner object in Java //message to be printed on the screen System.out.println("Please enter a number for getting its factorial"); a=sc.nextLong(); for(int x=1;x<=a;x++) //for loop for calculating the factorial { factorial=factorial*x; } System.out.println("the factorial of entered number is "+factorial); //message to be printed on screen } }

**Output1-**

please enter a number for getting its factorial

2

the factorial of entered number is 2

**Output2**

please enter a number for getting its factorial

4

the factorial of entered number is 24

**Explanation **

- As we know about the logic behind the factorial program in Java, unlike the previous example here, the value of a is taken from the user.
- In for loop, the value of a is entered by the user, and the value of x is initialised as 1, which is compared to the value of a that the user enters.
- Each iteration factorial variable stores the product of previous numbers with the incremented number.
- In each iteration, the value of x is increased by one using the increment operator ++.
- This is how for loop is used to make the factorial program in Java.

**Program to calculate the factorial of a number in Java using a while loop **

//creating a class named ExampleofFactorialpublic class ExampleofFactorial { public static void main(String[] args) { //main method //initialising and declaring the variable factorial by 1 int factorial = 1; int n = 1; //initialising and declaring the variable “n” by 1 //creating the object a of class Scanner Scanner a = new Scanner(System.in); //entering a number whose factorial is to be checked System.out.println("Please enter a number for getting its factorial value "); int number = a.nextInt(); //initialising variable //calculating the factorial of a given number using a while loop while( n <= number ){ factorial = factorial * i; n++; //incrementing the value of n in each iteration by 1 } //final message to be printed on the screen System.out.println("\nFactorial of entered number is " + number + " is: " + factorial); } }

**Output1**

please enter a number for getting its factorial value

5

Factorial of entered number is 120

**Output2**

please enter a number for getting its factorial value

4

Factorial of entered number is 24

**Output3**

please enter a number for getting its factorial value

3

Factorial of entered number is 6

**Explanation **

- In the above example, a loop is used where two variables, factorial and n are declared and initialised as 1.
- The number is a variable whose factorial is to be checked that the user enters. The value of n is initialised as 1, which is compared to the value of the number that the user enters.
- Each iteration factorial variable stores the product of previous numbers with the incremented number.
- And the value of n is increased by one using the increment operator.
- This is how while loop makes the factorial program in Java.

**Program to calculate the factorial of a number in Java using recursion **

//creating a class named ExampleofFactorial public class ExampleofFactorial { public static void main(String[] args) { //main method int a = 5; //initialising and creating variable long factorial = productofNumbers(a); //message to be printed on the screenSystem.out.println("so the factorial of " + a + " is= " + factorial); } public static long productofNumbers(int a) //function to be called { if (a >= 1) return a * productofNumbers(a - 1); else return 1; }}

**Output **

So the factorial of 5 is 120

**Explanation **

- In the above program, recursion is used to print the factorial value of a number. Recursion is a whole different thing that will be covered in a dedicated article. You must understand that recursion is a procedure that calls itself.
- In the above example, there are two functions. The first is the main function, and the second is the function “productofNumbers”, which is called by the main function and the value of “a” as an argument.
- Since the value of a 5 is greater than 1, the statements are written inside the if block gets executed. As a recursive function, it calls itself repeatedly until a value reaches 1.
- When the value of a becomes 1 at the last recursive call, the execution stops, and we get the output “So the factorial of 5 is 120”.

**Program to calculate the factorial of a number in Java using methods**

//creating a class named ExampleofFactorialclass ExampleofFactorial{ public static void main(String args[]) //main method { Scanner sc = new Scanner(System.in); //creating the object sc of scanner class //message to be displayed on the screen System.out.println("Please enter a number for getting its factorial value"); int a=sc.nextInt(); //intialsilsing and creating variable int factorial = fact(a); System.out.println("So the factorial of entered number "+a+" is = "+factorial); } static int fact(int y) //function to be called { int x,value=1; //creating and intiallising variables for(x=1;x<=y;x++) //for loop for calculating the factorial { value=value*i; } return value; //returning the factorial value }}

**Output**

please enter a number to get its factorial value

3

So the factorial of entered number 3 is = 6

**Explanation **

- In this program value of a is entered by the user as input.
- In the above example, there are two parts. The first is the main function, and the second one is the method that is to be called by the main function along with the value of “a” as an argument.
- After passing the argument to the second part, the factorial is calculated by the for loop and the output is displayed.

**Wrapping up **

Being concerned with the uses of factorial, there is no doubt that it is essential for solving problems related to probability, permutation, combination and the power series etc. use of factorial makes the complex calculator or methods easy to solve and understand. I hope this article will help you to have a

the clear understanding of factorial and its Java program.

*Contributed by Srashti Soni*

**About the Author**

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio