Monday 20 July 2020

Shell script to print n Fibonacci numbers


Program
echo "enter the number"
read n
echo "The Fibonacci numbers are"
f1=0
f2=1
echo "$f1"
echo "$f2"
n=`expr $n - 2`
while test $n -gt 0
do
   f3=`expr $f1 + $f2`
   echo "$f3"
   n=`expr $n - 1`
   f1=$f2
   f2=$f3
done



OUTPUT
enter the number
10
The Fibonacci numbers are
0
1
1
2
3
5
8
13
21
34

No comments:

Post a Comment