Shell Program to Calculate the Factorial of a Number
Last Updated :
01 Sep, 2021
Here we are going to see to calculate the factorial of a number. Factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n.
For example factorial of 5 is 5*4*3*2*1 which is 120.

Method 1: Using Recursive
Factorial can be calculated using the following recursive formula.
n! = n*(n-1)! \\n! = 1 \hspace{1 mm}if\hspace{1 mm} n = 0\hspace{1 mm} or\hspace{1 mm} n = 1
Below is implementation of factorial:
#!/bin/bash
# Recursive factorial function
factorial()
{
product=$1
# Defining a function to calculate factorial using recursion
if((product <= 2)); then
echo $product
else
f=$((product -1))
# Recursive call
f=$(factorial $f)
f=$((f*product))
echo $f
fi
}
# main program
# reading the input from user
echo "Enter the number:"
read num
# defining a special case for 0! = 1
if((num == 0)); then
echo 1
else
#calling the function
factorial $num
fi
Output:
Enter the number
5
120
Enter the number
3
24
Enter the number
6
720
Method 2: Using for loop
Approach:
- Get a number
- Use for loop to compute the factorial by using the below formula
- fact(n) = n * n-1 * n-2 * ...
- Display the result.
Below is the Implementation using for loop:
# shell script for factorial of a number
# factorial using for loop
echo "Enter a number"
# Read the number
read num
fact=1
for((i=2;i<=num;i++))
{
fact=$((fact * i))
}
echo $fact
Output:
Enter a number
5
120
Enter a number
7
5040
Enter a number
4
24
Method 3: using do-while loop
- Get a number
- Use do-while loop to compute the factorial by using the below formula
- fact(n) = n * n-1 * n-2 * .. 1
- Display the result.
Below is the Implementation using a while loop.
# shell script for factorial of a number
# factorial using while loop
echo "Enter a number"
# Read the number
read num
fact=1
# -gt is used for '>' Greater than sign
while [ $num -gt 1 ]
do
fact=$((fact * num))
num=$((num - 1))
done
# Printing the value of the factorial
echo $fact
Output:
Enter a number
10
3628800
Enter a number
2
2
Enter a number
9
362880
Similar Reads
Calculate the Factorial of a Number using Loop In this article, we will discuss how to calculate the factorial of a number with its working example in the R Programming Language using R while loop. In R programming, loops are essential constructs that allow us to repeat a set of instructions multiple times. The while loop is one such construct t
3 min read
How to Find the Factorial of a Number In mathematics, we often encounter the concept of factorial denoted as n! which represents the product of all integers less, than, or equal to a given non-negative integer n. Factorials find applications in combinatorics, probability, and other mathematical fields. In the R programming language, you
3 min read
Derivative of a Factorial Derivative of a factorial function can be explored through its connection to the Gamma function, which generalizes the concept of factorials to non-integer values. Since the factorial function is only defined for integers, we use the Gamma function, Î(n) = (n â 1)!, to extend the notion of factorial
4 min read
What is a Factorial Notation? Sometimes, to find order, arrangements, or combinations of objects are required. Combinatorics is that branch of mathematics that focuses on the study of counting. So the fundamental counting principle was introduced which states that if one event has m possible outcomes and the second event has n p
3 min read
Factorial in Maths The factorial of a natural number n indicates the number of ways n items can be arranged. It plays an important role in various mathematical concepts such as permutations, combinations, probability, and many others. For a positive integer n, the value of factorial is multiplication of all positive i
10 min read
Factorial Formula The factorial is one of the most fundamental mathematical operations in combinatorics, algebra, and number theory. Represented by an exclamation mark (!), the factorial of a non-negative integer n, denoted as n!, is the product of all positive integers less than or equal to n. It plays a crucial rol
8 min read