**Exercise:**

Write a Java Program to calculate factorial of a number.

#### 1.Compute Factorial using for loop.

**Click Here to View the Solution!**

```
public class FactorialCalculator {
public static void main(String[] args) {
int number = 13;
long factorial = 1;
for(int i = 1; i <= number; ++i)
{
// factorial = factorial * i;
factorial *= i;
}
System.out.printf("Factorial of %d = %d", number, factorial);
}
}
```

**Click Here to View the Output!**

Factorial of 13 = 6227020800

**Click Here to View the Explanation!**

- This program is used for finding the factorial of a number by using the for loop.
- A number 13 is stored into an integer variable
`number`

initially and a variable factorial is initialized as 1. - This program functions over a for loop and loops till the variable
`number`

hence going through all numbers till 13 while multiplying these numbers one by one and consistently storing the result into the variable factorial. - The data type
`long`

is used for storing the value in the variable factorial because the final value of the factorial is expected to be large and the data type int cannot store large values. - Finally, the value in the variable factorial is displayed along with
`number`

as “Factorial of 13 = 6227020800”.

#### 2.Compute Factorial using BigINTEGER.

**Click Here to View the Solution!**

`import`` java.math.BigInteger;`

` `

public` `

class FactorialCalculator `{`

` `

` `

public static void main(String[] args) `{`

` `

` `

int` number = 21;`

` BigInteger factorial = BigInteger.ONE;`

` `

for`(`

int` i = `

1`; i <= number; ++i)`

` {`

` factorial = factorial.multiply(BigInteger.valueOf(i));`

` }`

` System.out.printf(`

"Factorial of %d = %d"`, number, factorial);`

` }`

`}`

**Click Here to View the Output!**

Factorial of 21 = 51090942171709440000

**Click Here to View the Explanation!**

- This program utilizes the
`BigInteger`

instead of the long data type for the variable factorial for storing a large value. The variable`number`

is initialized as 21. - The
`BigInteger`

data type can be used through the use of a Java library`math.BigInteger`

and also a function,`multiply()`

instead of`*`

for calculating the factorial because “`*`

” cannot be used next to`BigInteger`

. - The multiplication of the of the factorial is done by casting the
`number`

to the`BigInteger`

and looping through all the numbers till`number = 21`

in the for loop. - The multiplied values are consistently stored into the variable factorial and the result is displayed on the screen.

#### 3.Compute Factorial using for loop

**Click Here to View the Solution!**

`public`` `

class FactorialCalculator `{`

` `

` `

public static void main(String[] args) `{`

` `

` `

int` number = `

6`, i = `

1`;`

` `

long` factorial = `

1`;`

` `

while`(i <= number)`

` {`

` factorial *= i;`

` i++;`

` }`

` System.out.printf(`

"Factorial of %d = %d"`, number, factorial);`

` }`

`}`

**Click Here to View the Output!**

Factorial of 6 = 720

**Click Here to View the Explanation!**

- This program is used for calculating the factorial of a number like the previous program but instead of the for loop,
`while loop`

is used. - Initially, a variable
`number`

is initialized as 6, a variable`i`

as 1 and a variable factorial as 1. - Like the above program using for loop, this program loops and increments the variable i,
`number`

of times inside the body of the`while loop`

and keeps multiplying its values and storing them into the variable factorial. - Both the
`for loop`

and`while loop`

are considered to be appropriate for performing this task however,`for loop`

is considered a better option since it knows the number of iterations till the variable`number`

. - Finally, the result of the factorial of the number 6 is displayed on the screen as “Factorial of 6 = 720”.