Fibonacci Series in Bash

Pr-requisite: Fibonacci Series

Write a program to print the Fibonacci sequence up to nth digit using Bash.
Examples:

Input : 5
Output :
Fibonacci Series is : 
0
1
1
2
3

Input :4
Output :
Fibonacci Series is : 
0
1
1
2



The Fibonacci numbers are the numbers in the following integer sequence .

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

Approach
As we know F0 = 0 and F1 = 1 and the next value comes by adding the previous two values .

 FN =  FN-1 +  FN-2

Loop to Nth number adding previous two numbers.

BASH

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program for Fibonacci
# Series
   
# Static input fo N
N=6
  
# First Number of the
# Fibonacci Series
a=0
  
# Second Number of the
# Fibonacci Series
b=1 
   
echo "The Fibonacci series is : "
   
for (( i=0; i<N; i++ ))
do
    echo -n "$a "
    fn=$((a + b))
    a=$b
    b=$fn
done
# End of for loop

chevron_right



Output:

Fibonacci Series is : 
0
1
1
2
3
5
8


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.