Write a program to Print Fibonacci Series in C++

In this post we will write a program to print Fibonacci series in C++ programming language. Let’s first understand what is Fibonacci Series.

What is Fibonacci Series?

In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:

1,;1,;2,;3,;5,;8,;13,;21,;34,;55,;89,;144,; ldots;

or (often, in modern usage):

0,;1,;1,;2,;3,;5,;8,;13,;21,;34,;55,;89,;144,; ldots; (sequence A000045 in OEIS).

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.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

F_n = F_{n-1} + F_{n-2},!,

with seed values

F_1 = 1,; F_2 = 1

or

F_0 = 0,; F_1 = 1.

Program to Print Fibonacci Series in C++

#include<iostream>

using namespace std;

main()
{
   int n, c, first = 0, second = 1, next;

   cout << "Enter the number of terms of Fibonacci series you want" << endl;
   cin >> n;

   cout << "First " << n << " terms of Fibonacci series are :- " << endl;

   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      cout << next << endl;
   }

   return 0;
}

 

OUTPUT:

print Fibonacci series

Leave a Comment