Fibonacci Series

Free Data Structures and Algorithms courses with real-time projects Start Now!!

Fibonacci Series is one of the most intriguing series in mathematics. The numbers in series are called Fibonacci numbers.

The first two numbers in the Fibonacci series are 0 and 1. For all next numbers, each Fibonacci number is the sum of its 2 previous numbers.

Fn=Fn-1 + Fn-2 , where Fn is the next number in the series.

Algorithm

Step 1: Set Variables a=0, b=1, c=0
Step 2: Print a,b
Step 3: c=a+b
Step 4: Print c
Step 5: set a=b,b=c
Step 6: Set A = B, B = C
Step 7: REPEAT steps 4 – 6, for n times

Pseudocode of Fibonacci Series

Procedure Fibonacci: n

Display 0, 1

Set a=0,b=1

Display a,b

While the number of terms is less than n
         
         c=a+b
         Display c
         Set a=b, b=c

End procedure

Implementation of Fibonacci series in C

#include <stdio.h>
int main() 
{

  int i, n;
  int a = 0, b= 1;
  int c=a+b;

  printf("Enter the number of terms: ");
  scanf("%d", &n);

  printf("Fibonacci Series: %d, %d, ", a,b);

  for (i = 3; i <= n; ++i) 
  {
    printf("%d, ", c);
    a=b;
    b=c;
    c=a+b;
  }

  return 0;
}

Implementation of Fibonacci series in C++

#include <iostream>
using namespace std;

int main() 
{

  int i, n;
  int a = 0, b= 1;
  int c=a+b;

  cout << "Enter the number of terms: ";
  cin >> n;

  cout << "Fibonacci Series: " << a << ", "<< b;

  for (i = 3; i <= n; ++i) 
  {
    cout << " " << c;
    a=b;
    b=c;
    c=a+b;
  }

  return 0;
}

Fibonacci series implementation in JAVA

import java.io.*;
public class Fibonacci
{  
public static void main(String args[])throws IOException
{    
 int a=0,b=1,c,i,n; 
 BufferedReader ob = new BufferedReader(new InputStreamReader(System.in));
 
 n=Integer.parseInt(ob.readLine());
 
 System.out.print(a+", "+b);    
    
 for(i=2;i<n;++i)    
 {    
  c=a+b;    
  System.out.print(", "+c);    
  a=b;    
  b=c;    
 }    
}
}

Implementation of Fibonacci series in Python

n = int(input("no. of terms: "))
a=0
b=1
c=0
i=0
print("Fibonacci sequence:")
print (a)
print(b)

while i < n-2:
       
       c = a+b
       print(c)
       a = b
       b = c
       i=i+1

Conclusion

Fibonacci series is one of the most used series from a mathematical standpoint. Fibonacci Retracements are used by traders, Fibonacci numbers are found in every part of history, right from Vedic mathematics to the biological real world.

In this article, we learned how to generate Fibonacci series using different programming languages. The next article will contain hash tables, their implementation and applications in real life.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *