Print Fibonacci series in C++

In this tutorial, we will learn to print the Fibonacci series in C++ program. Basically, this series is used in mathematics for the computational run-time analysis. So, today we will get to know about the Fibonacci series, a method to find this series, and a C++ program that prints ‘n’ terms of the series.

Fibonacci series

A Fibonacci series is a series in which every term is the sum of its previous two terms. The first and second term of this series is 0 and 1 respectively which means to get the third term, we have to add the first and second term i.e. 0 and 1. So, the third term will be 1. Likewise, to get the next term, we have to add the previous two terms.

The Fibonacci series is –

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

In mathematics, the series is represented as F(n) where ‘n’ is the nth term of the series.

F(1) = 0
F(2) = 1
F(n) = F(n-1) + F(n-2)

Logic to print the series

We can find the terms of the Fibonacci series using two logical methods –

  1. Without using recursion
    In this method, store the first two terms (0 and 1) in an array which can store only two integers. Then add the array contents and store in a third variable. After displaying the term which is at first position of the array, shift the contents of the array to left and transfer the content of the third variable to the last index of the array. Repeat these steps ‘n’ times, where ‘n’ is the number of terms to be displayed.
  2. Using a recursive function
    Also, we can find terms using a recursive function which will call itself repeatedly on the basis of a base or termination condition.

But, in this tutorial, we will implement the first method i.e. without using a function.

C++ program to print ‘n’ terms of the Fibonacci series

Now, we will see a C++ program that displays the Fibonacci series up to n terms. We will take the number of terms from the user as an input. So, the program is as follows –

#include<iostream>
using namespace std;
int main()
{
  int series[]={0,1},terms,temp;
  cout<<"\nENTER NUMBER OF TERMS : ";
  cin>>terms;
  cout<<"THE SERIES IS : ";
  for(int i=0;i<terms;++i)
  {
    cout<<series[0]<<" ";
    temp=series[0]+series[1];
    series[0]=series[1];
    series[1]=temp;
  }
  return 0;
}

In the above program, we define an array ‘series’ which contains the first two terms of the series i.e. 0 and 1. Then we are taking the number of terms from the user. A for loop executes which prints a term in each iteration. In the loop, we display the first element of the array. And we add the array contents and store the result in the ‘temp’ variable. To continue displaying the successive terms of the series, we shift the array contents to the left and store the ‘temp’ variable value at the last index of the array. So, the loop iterates and prints ‘n’ terms of the series where ‘n’ is the number of terms entered by the user.

C++ program output

The above C++ program displays ‘n’ terms of the Fibonacci series. The output of the program is –

siddharth@siddharth-Lenovo-Y520-15IKBN:~/cpp$ g++ fibo.cpp
siddharth@siddharth-Lenovo-Y520-15IKBN:~/cpp$ ./a.out

ENTER NUMBER OF TERMS : 6
THE SERIES IS : 0 1 1 2 3 5 
siddharth@siddharth-Lenovo-Y520-15IKBN:~/cpp$

Here, the number of terms entered by the user is 6. Thus, the program displays the first six terms of the series.

Also read: C++ program to find solutions of quadratic equation

Print a particular nth Fibonacci number in C++ up to range 10^18

Leave a Reply

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