An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Tuesday, August 25, 2009

Shell Scripting #0017: Factorial - How to create Factorial Program in Shell Script?

No comments :
All these while, I was under an impression that my blog is helpful only to professionals. Not anymore, as one of a student asked me how to create factorial program in Shell Script.


This is for him, Thank you Vignesh for reaching out to me.

Factorial

#!/bin/bash
n=0
on=0
fact=1

echo -n "Enter number to find factorial : "
read n

on=$n

while [ $n -ge 1 ]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done

echo "Factorial for $on is $fact"

No comments :