Tuesday, January 24, 2023

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

 6. Print fibonacci series.

PROGRAM:

echo "enter number of elements of the fibonacci series:"
read n
a=0
b=1
if [ $n -eq 0 ]
then
    echo "no elemnts"
else
    if [ $n -eq 1 ]
    then
        echo 0
    else
        echo 0
        echo 1
        c=2
        while [ $c -ne $n ]
        do
            d=`expr $a + $b`
            echo $d   
            a=$b
            b=$d
            c=`expr $c + 1`
        done
    fi
fi

OUTPUT:

[bgc@localhost ~]$ sh 7.sh
enter number of elements of the fibonacci series:
6
0
1
1
2
3
5
[bgc@localhost ~]$

No comments:

Post a Comment