The Wayback Machine - https://web.archive.org/web/20240131161543/https://www.geeksforgeeks.org/fibonacci-series-bash/
Open In App
Related Articles

Fibonacci Series in Bash

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Prerequisite: 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




# Program for Fibonacci
# Series
  
# Static input for 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


Output:

Fibonacci Series is : 
0
1
1
2
3
5
8

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 20 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads