Fibonacci Series Program in C++

Fibonacci Series Program in C++ | In the Fibonacci series, the next element will be the sum of the previous two elements.

The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on…

It is named after an Italian mathematician, Leonardo Fibonacci, who lived in the early thirteenth century. Fibonacci numbers are a series in which each number is the sum of the previous two numbers.

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

As a rule, the expression is Xn= Xn-1+ Xn-2

C++ Program to Display Fibonacci Series up to N Number of terms

// Fibonacci Series up to n number of terms
// C++ program to Display Fibonacci Series
#include<iostream>
using namespace std;
int main()
{
  // declare variables
  int n, i, a=0, b=1, c;

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

  // display Fibonacci Series
  cout << "Fibonacci Series is: " << endl;
  for (i=a; i<=n; i++)
  {
    cout << a << "  ";
    c=a+b;
    a=b;
    b=c;
  }

  return 0;
}

Output:-

Enter the number of terms: 10
Fibonacci Series is:
0 1 1 2 3 5 8 13 21 34 55

Enter the number of terms: 15
Fibonacci Series is:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

Display Up to a Given Number

// Program to Generate Fibonacci Sequence
// Up to a Certain Number
#include<iostream>
using namespace std;
int main()
{
  // declare variables
  int n, a=0, b=1, c;

  // take input
  cout << "Enter Range: ";
  cin >> n;

  // display Fibonacci Series
  cout << "Fibonacci Series is: " << endl;
  while ( a <= n )
  {
    cout << a << "  ";
    c=a+b;
    a=b;
    b=c;
  }

  return 0;
}

Output:-

Enter Range: 100
Fibonacci Series is:
0 1 1 2 3 5 8 13 21 34 55 89

C++ Program to find Nth Fibonacci term

// Program to Generate Fibonacci Sequence 
// Up to a Certain Number
#include<iostream>
using namespace std;
int main()
{
  // declare variables
  int term;
  int t1 = 0; // first term
  int t2 = 1; // second term
  int n = 0; // nth term

  // take input
  cout << "Enter term: ";
  cin >> term;

  if(term==1)
  {
    cout << "Fibonacci term is = " << t1 << endl;
    return 0;
  }
  if(term==2)
  {
    cout << "Fibonacci term is = " << t2 << endl;
    return 0;
  }

  // 2 terms are already exist so
  // iterator variable should
  // start with 3
  for(int i=3; i <= term; i++)
  {
     // update term values
     n = t1 + t2;
     t1 = t2;
     t2 = n;
  }

  // display result
  cout << "Fibonacci term is = " << n << endl;

  return 0;
}

Output:-

Enter term: 5
Fibonacci term is = 3

Enter term: 7
Fibonacci term is = 8

Enter term: 10
Fibonacci term is = 34

Fibonacci Series using Recursion

We can also use the recursion technique to display the Fibonacci series. A technique of defining the method/function that contains a call to itself is called the recursion.

The recursive function/method allows us to divide the complex problem into identical single simple cases that can be handled easily. This is also a well-known computer programming technique: divide and conquer.

The base case for finding factorial
fibonacci(0) = 0
fibonacci(1) = 1

General case for finding factorial
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)

Recursive function for find nth Fibonacci term

int fibonacci(int n)
{
if(n<=1)
return n; // base case
else // general case
return (fibonacci(n-1) + fibonacci(n-2) );
}

Fibonacci recursive method using ternary operator,

Using a ternary operator the logic of the Fibonacci recursive method can be written within a single line.

int fibonacci(int n)
{
return (n<=1) ? n : (fibonacci(n-1) + fibonacci(n-2) );
}

Display Nth Fibonacci term using Recursion

// Fibonacci Series up to n number of terms
// C++ program to Display Fibonacci Series
#include<iostream>
using namespace std;
int fibonacci(int);
int main()
{
  // declare variables
  int n;

  // take input
  cout << "Enter term: ";
  cin >> n;

  // display result
  cout << "Fibonacci term= " << fibonacci(n) << endl;

  return 0;
}

// recursive function to find fibonacci term
int fibonacci(int n)
{
  if(n <= 1) return n; // base case
  else // general case
  return (fibonacci(n-1) + fibonacci(n-2) );
}

Output:-

Enter term: 7
Fibonacci term= 13

Enter term: 10
Fibonacci term= 55

Enter term: 15
Fibonacci term= 610

Fibonacci Series up to N Number of terms using Recursion

// Fibonacci Series up to n number of terms
// C++ program to Display Fibonacci Series
#include<iostream>
using namespace std;
int fibonacci(int);
int main()
{
  // declare variables
  int n;

  // take input
  cout << "Enter N value: ";
  cin >> n;

  // display result
  cout << "Fibonacci Series, " << endl;
  for(int i=0; i < n; i++)
  cout << fibonacci(i) << "  ";

  return 0;
}

// recursive function to find fibonacci term
int fibonacci(int n)
{
  if(n <= 1) return n; // base case
  else // general case
  return (fibonacci(n-1) + fibonacci(n-2) );
}

Output:-

Enter N value: 10
Fibonacci Series,
0 1 1 2 3 5 8 13 21 34

Enter N value: 20
Fibonacci Series,
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply