Search Here

Shell Script to implement a recursive shell function to find Fibonacci numbers up to a given n


fibo()
{
    if(( $1==1 ))
    then
        echo 0
    elif (( $1==2 ))
    then
        echo 1
    else
        (( t=$1-1 ))
        a=`fibo $t`
       
        (( t=$1-2 ))
        b=`fibo $t`
       
        (( res=a+b ))
        echo $res
    fi
}

echo -e "Enter an integer: \c"
read n
if(( n>0 ))
then
    for(( i=1;i<=n;i++ ))
    do
        echo -e "`fibo $i`  \c"
    done
    echo
else
    echo ERROR: Invalid input given   
fi

No comments:

Post a Comment