Tuesday, January 24, 2023

Unix Shell Programming Example for B.Sc, B.Tech, BCA, MCA

 5. Find out the factorial of a number.

PROGRAM:

echo "Enter the number:"
read n
fact=1
if [ $n -eq 0 ]
then
    echo "fact = "1
else
   
        for i in `seq 1 $n`
        do
            fact=`expr $fact \* $i`
        done
        echo "fact = $fact"

fi

OUTPUT:

[bgc@localhost ~]$ sh 6.sh
Enter the number:
3
fact = 6
[bgc@localhost ~]$ sh 6.sh
Enter the number:
2
fact = 2
[bgc@localhost ~]$ sh 6.sh
Enter the number:
0
fact = 1
[bgc@localhost ~]$

No comments:

Post a Comment