Thursday, March 26, 2015

Factorial Value

Algorithm
Step 1 : Start
Step 2 : Read number
Step 3 : Initialize 1 to fact and number to i
Step 4 : fact = fact * i
Step 5 : Decrement i by 1
Step 6: Repeat steps 4–6 until i > 0
Step 7 : Print fact
Step 8 : Stop

Program (fact.sh)
# Factorial value using until
echo -n "Enter a positive number : "
read n
f=1
until [ $n -lt 1 ]
do
f=`expr $f \* $n`
n=`expr $n - 1`
done
echo "Factorial value : $f"

Output
[vijai@localhost loops]$ sh fact.sh
Enter a positive number : 10
Factorial value : 3628800

No comments:

Post a Comment