Wednesday 28 September 2011


CALCULATING FACTORIAL OF A GIVEN NUMBER

Aim :
To write a shell program to find the factorial of a given number.

Algorithm :

1.Read the number.
2.Initialize the value of i and factorial f to 1.
3.While the value of i is less than the number find the value of factorial by multiplying i and f.
4.Increment the value of i by 1.
5. Display the result.

Program
#Factorial program
echo Enter a number
read n
i=1
f= 1
if test $n -eq 0
then
echo factorial is 1
else
while test $i -le $n
do
f= ‘expr $f\ * $1’
i=expr$i+ 1’
done
echo Factorial is $f
fi

Output:
Enter a number
5
Factorial is 120

Result:
Thus the program is
written and executed to find the factorial of the given
number.

0 comments:

Post a Comment