[Linux][Shell script] print fibonacci series of 12 numbers


#!/bin/sh
# Fibonacci series – eg:- “0 1 1 2 3 5 8 13 21 34 55 …”

a=0 #First value of the series
b=1 #Second value of the series
#print the first value of the series.
echo $a
#print the second value of the series.
echo $b
#Now a small mathmatics for finding other numbers in the series .

for I in 0 1 2 3 4 5 6 7 8 9 10 #You can find upto any number . Here it is upto 10 more numbers in the series .
do
# add a and b and assign to c
c=$(($a+$b))
#print c which is the next number in the Fibonacci series
echo $c
#Now assign b to a
a=$b
#Assign c to b
b=$c
done

Published by RNP

A person who likes learning new languages.

Leave a comment