B.Sc Computer Science with Data analytics
Linux and Shell Programming - Lab
Bharathiar University
Program 10- Write a shell script to find the factorial of given integer.
Source Code
# Function to calculate factorial
factorial() {
num=$1
fact=1
while [ $num -gt 1 ]; do
fact=$((fact * num))
num=$((num - 1))
done
echo $fact
}
# Main program starts here
# Prompt the user to enter the number
echo "Enter a number to find factorial: "
read n
# Calculate the factorial of the given integer
result=$(factorial $n)
echo "The factorial of $n is: $result"
Sample OUTPUT:
No comments:
Post a Comment