Algorithm
Step 1 : Start
Step 2 : Read number of terms as n
Step 3 : Initialize 0 to f1, 1 to f2 and 2 to i
Step 4 : Print initial fibonacci terms f1, f2
Step 5 : Generate next term using the formula f3 = f1 + f2
Step 6 : Print f3
Step 7 : Increment i by 1
Step 8 : Assign f2 to f1
Step 9 : Assign f3 to f2
Step 10 : Repeat steps 5–9 until i n
Step 11 : Stop
Program (fibo.sh)
# Fibonacci series using while loop
echo -n "Enter number of terms : "
read n
echo "Fibonacci Series"
f1=0
f2=1
echo -n "$f1 "
echo -n " $f2 "
i=2
while [ $i -lt $n ]
do
f3=`expr $f1 + $f2`
echo -n " $f3 "
f1=$f2
f2=$f3
i=`expr $i + 1`
done
echo
Output
[vijai@localhost loops]$ sh fibo.sh
Enter number of terms : 8
Fibonacci Series
0 1 1 2 3 5 8 13
Step 1 : Start
Step 2 : Read number of terms as n
Step 3 : Initialize 0 to f1, 1 to f2 and 2 to i
Step 4 : Print initial fibonacci terms f1, f2
Step 5 : Generate next term using the formula f3 = f1 + f2
Step 6 : Print f3
Step 7 : Increment i by 1
Step 8 : Assign f2 to f1
Step 9 : Assign f3 to f2
Step 10 : Repeat steps 5–9 until i n
Step 11 : Stop
Program (fibo.sh)
# Fibonacci series using while loop
echo -n "Enter number of terms : "
read n
echo "Fibonacci Series"
f1=0
f2=1
echo -n "$f1 "
echo -n " $f2 "
i=2
while [ $i -lt $n ]
do
f3=`expr $f1 + $f2`
echo -n " $f3 "
f1=$f2
f2=$f3
i=`expr $i + 1`
done
echo
Output
[vijai@localhost loops]$ sh fibo.sh
Enter number of terms : 8
Fibonacci Series
0 1 1 2 3 5 8 13
No comments:
Post a Comment