Monday, January 9, 2012

Shell Script To find Factorial of a Number


Q. How do I write Shell Script to find factorial of a number
Ans:

#!/bin/bash
fact=1
#taking input from user
echo -e "enter a number"
read n
#if enter value less than 0
if [ $n -le 0 ] ; then
echo "invalid number"
exit
fi
#factorial logic
if [ $n -gt 0 ] ; then
for((i=$n;i>=1;i--))
do
fact=`expr $fact \* $i`
done
fi
echo "The factorial of $n is $fact"


Output

Enjoy :)

0 comments:

Post a Comment