how do i code the shell script?

$ cat fib
#!/bin/bash
Num=${1-5}
a=0
b=1
unset s

printf "Result: "
for (( i=0; i<Num; i++ )); do
    printf "${s:+ + }$a"
    s=$(($s + $a))
    n=$((a+$b))
    a=$b
    b=$n
done
echo " = $s"
$ ./fib 5
Result: 0 + 1 + 1 + 2 + 3 = 7
$ ./fib 10
Result: 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 = 88

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top