C++ program to display Fibonacci series

In this article, you will learn about a C++ program to display Fibonacci series. The series begins with 0 and 1.  And the next number in the series is the sum of the previous two numbers. This sequence in the series goes on to infinity.

By following the above rule the Fibonacci sequence looks like

 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

Example: Fibonacci series in C++

//C++ program to display Fibonacci series
#include <iostream>  
using namespace std;  
int main() {  
  int num1=0,num2=1,num3,n;    
 cout<<"Enter the total number of elements: ";    
 cin>>n;    
 cout<<num1<<" "<<num2<<" "; 
 for(int i=2;i<n;++i)
 {    
  num3=num1+num2;    
  cout<<num3<<" ";    
  num1=num2;    
  num2=num3;    
 }    
   return 0;  
   } 

Output

cpp program to display Fibonacci series

Recommended Articles

C++ student grade program

C++ program to print star patterns

Scroll to Top