## Java – Find Factorial of a Number

In Java, you can find the factorial of a given number using looping statements or recursion techniques.

In this tutorial, we shall learn how to write Java programs to find factorial of a given number.

Following picture has the formula to calculate the factorial of a number. And also factorial examples for numbers 5 and 7.

### Example 1 – Factorial using While Loop

In this example, we shall make use of Java While Loop, to find the factorial of a given number.

#### Algorithm

We shall implement the following factorial algorithm with while loop.

1. Start.
2. Take number in a variable n. [We have to find factorial for this number.]
3. Initialize variable factorial with `1`.
4. Initialize loop control variable i with `1`.
5. Check if i is less than or equal to n. If the condition is false, go to step 8.
6. Multiply factorial with i.
7. Increment i. Go to step 5.
8. Print factorial.
9. Stop.

Java Program

```/**
* Java Program - Factorial
* Factorial of n is n! = 1.2.3....(n-1).n
* n should be >= 0
*/

public class Factorial {

public static void main(String[] args) {
//number
int n = 5;

//store factorial in this
int factorial = 1;

//compute factorial
int i=1;
while(i<=n) {
factorial *= i; //factorial = factorial * i
i++;
}

System.out.print(n+"! = "+factorial);
}
}```

Run the above Java program, and you shall get the following output.

`5! = 120`

### Example 2 – Factorial using For Loop

In this example, we shall use Java For Loop to find the factorial of a given number. The algorithm would be same as that of the one used in above example.

Java Program

```/**
* Java Program - Factorial
* Factorial of n is n! = 1.2.3....(n-1).n
* n should be >= 0
*/

public class Factorial {

public static void main(String[] args) {
//number
int n = 5;

//store factorial in this
int factorial = 1;

//compute factorial
for(int i=1;i<=n;i++) {
factorial *= i; //factorial = factorial * i
}

System.out.print(n+"! = "+factorial);
}
}```

Run the above program, and you shall get the following output for `n=5`.

`5! = 120`

### Example 3 – Factorial using Recursion

Finding Factorial of a number is a classic example for recursion technique in any programming language.

In this example, we shall use recursion and the factorial.

Java Program

```/**
* Java Program - Factorial
* Factorial of n is n! = 1.2.3....(n-1).n
* n should be >= 0
*/

public class Factorial {

public static void main(String[] args) {
//number
int n = 5;

System.out.print(n+"! = "+factorial(n));
}

/**
* Computes Factorial of a number recursively
*/
static int factorial(int n) {
if (n==0) {
return 1;
} else {
return n*factorial(n-1);
}
}
}```

Following is the output to this Java program.

`5! = 120`

### Example 4 – Factorial using Ternary Operator

When using recursion technique, instead of if else as in above example, you can also use ternary operator.

In this example, we shall use recursion technique with ternary operator to make the code concise.

Java Program

```/**
* Java Program - Factorial
* Factorial of n is n! = 1.2.3....(n-1).n
* n should be >= 0
*/

public class Factorial {

public static void main(String[] args) {
//number
int n = 5;

System.out.print(n+"! = "+factorial(n));
}

/**
* Computes Factorial of a number recursively and uses ternary operator
*/
static int factorial(int n) {
return (n == 0) ? 1 : n * factorial(n - 1);
}
}```

Run the program to find factorial of `5`. You can use the `factorial()` , from the above program, function in your program and call it, to find the factorial of any given `n`.

`5! = 120`

### Conclusion

In this Java Tutorial, we learned how to write Java programs to find the factorial of a given number using loop statements and recursion technique.