Find nCr.
Program
echo -e "Enter n: \c"
read n
echo -e "Enter r: \c"
read r
i=2
nf=1
temp=`expr $n - $r`
tf=1
rf=1
ncr=1
while test $i -le $n
do
nf=`expr $nf\*$i`
i=`expr $i + 1`
done
nf=`echo "$nf" | bc`
i=2
while test $i -le $temp
do
tf=`expr $tf\*$i`
i=`expr $i + 1`
done
tf=`echo "$tf" | bc`
i=2
while test $i -le $r
do
rf=`expr $rf\*$i`
i=`expr $i + 1`
done
rf=`echo "$rf" | bc`
temp2=`echo "$nf / $tf" | bc`
ncr=`echo "$temp2 / $rf" | bc`
echo "nCr = $ncr"
Output
sh ncr.sh
Enter n: 5
Enter r: 4
nCr = 5
sh ncr.sh
Enter n: 9
Enter r: 4
nCr = 126
sh ncr.sh
Enter n: 12
Enter r: 5
nCr = 792