Hey guys!! Welcome to flower brackets blog. In this post we are going to learn factorial java.

#### What is factorial?

Factorial is nothing but the product of all numbers less than or equal to given number. For example

5! = 5 * 4 * 3 * 2 * 1 = 120 (5! means 5 factorial)

4! = 4 * 3 * 2 * 1 = 24 (4! means 4 factorial)

Now let’s see java program to find factorial of a number,

#### Factorial program in java using for loop

Here let’s see how to write a java program to find factorial of a number in java using for loop,

public class FactorialDemo { public static void main(String[] args) { int number = 6, factorial = 1; for(int a = 1; a <= number; a++) { factorial = factorial * a; } System.out.println("Factorial of " + number + " is : " + factorial); } }

**Output:**

Factorial of 6 is : 720

#### Java program to find factorial value with using recursion

public class FactorialRecursion { public static void main(String[] args) { int factorial = 1; int number = 6; factorial =factorialFunction(number); System.out.println("Factorial of " + number + " is : " + factorial); } static int factorialFunction(int num) { if(num == 0) { return 1; } else { return(num *factorialFunction(num - 1)); } } }

**Output:**

Factorial of 6 is : 720

#### Java program to find factorial using while loop

Now we are going to learn factorial program in java using while loop,

Also Read – Sum And Average Of Values In An Array In Java

import java.util.Scanner; public class FactorialWhile { public static void main(String[] args) { int number, factorial = 1; Scanner sc = new Scanner(System.in); System.out.println("Please enter a number to find its factorial : "); number = sc.nextInt(); int a = 1; while(a <= number) { factorial = factorial * a; a++; } System.out.println("Factorial of " + number + " is : " + factorial); sc.close(); } }

**Output:**

Please enter a number to find its factorial : 5

Factorial of 6 is : 120

#### Factorial program in java using do while loop

Let’s write a program to print factorial of any number using do while loop,

import java.util.Scanner; public class FactorialDoWhile { public static void main(String[] args) { int number, factorial = 1; Scanner sc = new Scanner(System.in); System.out.println("Please enter a number to find its factorial : "); number = sc.nextInt(); int a = 1; do { factorial = factorial * a; a++; } while(a <= number); System.out.println("Factorial of " + number + " is : " + factorial); sc.close(); } }

**Output:**

Please enter a number to find its factorial : 7

Factorial of 7 is : 5040

##### Conclusion

That’s it guys. This is all about factorial program in java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.