Sunday, March 11, 2012

FACTORIAL OF A NUMBER --- unix lab


FACTORIAL OF A NUMBER


AIM:
            To write a Shell Program to find the factorial of a Number.

ALGORITHM:

Step 1: Start

Step 2: Read the Number as ‘n’

Step 3: Assign the value of ‘p’ as 0 and solve the expression i = n-1

Step 4: While the value of ‘i’ is greater than or equal to 1 do the following steps.

Step 5: Solve the expression n=n*1 and i=i-1 (Decrementing Operation)

Step 6: Then print the value of ‘n’ as the factorial of the given number.

Step 7: Stop.

SHELL PROGRAM:

echo "Enter a Number"
read n
i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n"

SAMPLE OUTPUT:

[Cseb19@sathish-desktop]# sh fact.sh
Enter a Number
5
The Factorial of the given Number is 120

RESULT:
            Thus the Shell Program to find the factorial of the Number was executed and the output was verified.

No comments:

Post a Comment