**Java program to find factorial of a number **is a simple program which is program to find factorial of number in java.

In this tutorial** we will see factorial program in java, Factorial of a number** is the mathematical problem.

**Factorial of a number **

Factorial of a number N is calculated as N!.

**Syntax to find factorial of a number**

**Factorial Formula = ****N!** => N*(N-1)*(N-2)….

**Example: **

Find factorial

**calculate 6 factorial**

**6! =6*5*4*3*2*1 =720**

Factorial of a number 6 is 720.

Using this technique we implement the **factorial in java**.

We implement **factorial program in java **in three ways as :

**First, Factorial using for loop****Second, Factorial using While loop**

**Factorial using for loop**

This is first method using which we implement the factorial.

In this technique, we use the iterative loop as **for loop** to iterate and multiply the number up to N-1.

**Java program for factorial**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.*; public class Main { public static void main(String[] args) { int c, n, fact = 1; Scanner sc=new Scanner(System.in); System.out.println("Enter a number to calculate its factorial"); n=sc.nextInt(); for (c=1;c<=n;c++) fact=fact*c; System.out.println("Factorial of "+n+" = "+fact); } } |

**Output :**

**Explanation :**

**Factorial using for loop**

**1. Execution starts with the main method.**

**2. Declare variables**

c,fact = > temporary variables

n => to store number

**3. First we take input number which user want to find factorial**

n=sc.nextInt(); => n=3

**4. Next we calculate the factorial using for loop**

for (c=1;c<=n;c++)

**At Iteration 1 :**

c=1, 1<=3 condition true loop executed

fact=fact*c; => fact= 1

**At Iteration 2 :**

c=2, 2<=3 condition true loop executed

fact=fact*c; => fact= 2

**At Iteration 3 :**

c=3, 3<=3 condition true loop executed

fact=fact*c; => fact= 6

**At Iteration 4 :**

c=4, 4<=3 condition false

**5. Print the factorial of number.**

=> System.out.println(“Factorial of “+n+” = “+fact);

**Factorial using while loop**

This is another method using which we implement the factorial.

In this technique, we use the iterative loop as **while loop** to iterate and multiply the number up to N-1.

## Factorial of a number using while loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.*; public class Main { public static void main(String[] args) { int c=1, n, fact = 1; Scanner sc=new Scanner(System.in); System.out.println("Enter a number to calculate its factorial"); n=sc.nextInt(); while(c<=n) { fact=fact*c; c++; } System.out.println("Factorial of "+n+" = "+fact); } } |

**Output :**

**Explanation :**

**Factorial using while loop**

**1. Execution starts with the main method.**

**2. Declare variables**

c,fact = > temporary variables

n => to store number

**3. First we take input number which user want to find factorial**

n=sc.nextInt(); => n=3

**4. Next we calculate the factorial using for loop**

while(c<=n)

**At Iteration 1 :**

c=1, 1<=3 condition true loop executed

fact=fact*c; => fact= 1

**At Iteration 2 :**

c=2, 2<=3 condition true loop executed

fact=fact*c; => fact= 2

**At Iteration 3 :**

c=3, 3<=3 condition true loop executed

fact=fact*c; => fact= 6

**At Iteration 4 :**

c=4, 4<=3 condition false

**5. Print the factorial of number.**

=> System.out.println(“Factorial of “+n+” = “+fact);

## Leave a Reply