Saturday, 16 July 2016

Write a shell script in Linux/Unix to find out the value of nCr.


clear
echo “ Enter values for n & r”
read n
read r
t=0
i=1
a=1
b=1
c=1
d=0
while [ $i –le $n ]
do
a=`expr $a \* $i`
i=`expr $i + 1`
done
i=1
while [ $i –le $r ]
do
b=`expr $b \* $i`
i=`expr $i + 1`
done
i=1
d=`expr $n - $r`
while [ $i –le $d ]
do
c=`expr $c \* $i`
i=`expr $i + 1`
done
i=`expr $b \* $c`
t=`expr $a / $i`
echo “ The Result Is = $t”

OUTPUT:

Enter values for n & r
4
2
The Result Is = 6



















Write a shell script in Linux/Unix to find Factorial of a given number.


echo "ENTER A NUMBER-"
read n
fct=1
i=1
while test $i -le $n
do
fct=`expr $fct \* $i`
i=`expr $i + 1`
done
echo "THE FACTORIAL OF THE NUMBER IS -" $fct


OUTPUT:

ENTER A NUMBER-
5
THE FACTORIAL OF THE NUMBER IS-120







Write a shell script in Linux/Unix to generate Fibonacci series.


tput clear
echo enter the range of the fibonacci series----
read n
echo -------------------------------------------------
a=0
b=1

i=2
echo $a
echo $b
while test $i -lt $n
do

c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done


OUTPUT:

enter the range of the fibonacci series----
9
-------------------------------------------------
0
1
1
2
3
5
8
13
21











Popular Posts