# Factorial of given number using Shell Script

**Factorial of given number Shell Script**

**Problem Statement :** Write a Shell program to find the Factorial of given number using Shell Script code.

Factorial of given number using Shell Script program code will ask user to enter number for which user want to calculate factorial . After calculating the factorial it will be printed to screen output.

Program is using while loop which is starting from entered number to 1 . It will then calculate the factorial of number using this method.

Algorithm for Finding Factorial of Number

Start with the entered number $n towards one . Multiply number $n by another variable which starts with one by reducing $n by one in each step.

1 2 3 4 5 6 7 8 9 |
echo “enter a number”; read n; i=1; while [ $n -ne 0 ] do i=’expr $n\* $i’ n=’expr $n-1’ done echo “factorial of given no. is $i” |

**Factorial of given number using Shell Script Output **

**Sample Output 1**

nulls@ubuntu:~$ ./script.sh

enter a number

3

factorial of given no. is 6

**Sample Output 2**

nulls@ubuntu:~$ ./script.sh

enter a number

5

factorial of given no. is 120

**Sample Output 3**

nulls@ubuntu:~$ ./script.sh

enter a number

7

factorial of given no. is 5040

**Sample Output 4
**

nulls@ubuntu:~$ ./script.sh

enter a number

7

factorial of given no. is 5040

**Sample Output 5
**

nulls@ubuntu:~$ ./script.sh

enter a number

7

factorial of given no. is 5040