9+ CPP Programs to Print Fibonacci Series

This post contains a total of 9+ CPP Program Examples with Source Code to Print Fibonacci Series of a Number. All these Fibonacci Series programs are made using C++.

You can use the source code of these examples with credits to the original owner.

Related Posts

CPP Programs to Print Fibonacci Series

1. By Angelin Blessy

Made by Angelin Blessy. Simple Fibonacci Series Program. Source

Enter counting numbers: 8
Fibonacci Series : 1 1 
2 3 5 8 13 21 
#include <iostream>
using namespace std;

int main()
 {
 int a=1, b=1, c=0, limit;
    cout<<"Enter counting numbers: "<<endl;
    cin>>limit;
    if(limit>1)
    {
    cout<<"Fibonacci Series : "<<a<<" "<<b<<" "<<endl;// first two term
    c=a+b;
    limit=limit-2;   // decrease the limit by 2. since two numbers already printed
    while(limit)
    {
        cout<<c<<" ";
        a=b;
        b=c;
        c=a+b;
        limit--;
    }
    }
    else
    {
    cout<<"1";
    }
 }

2. By Rj

Made by Rj. Basic C++ Program to Print Fibonacci Series. Source

Enter a number 10
1
2
3
5
8
13
21
34
55
89
144
233
#include <iostream>
using namespace std;

int main() {

int n1=1,n2=2,n3,n,i;

cout<< "Enter a number" <<endl;
cin>>n;
cout<<n1<<endl<<n2<<endl;

    for(i=1;i<=n;i++)
    {
        n3=n1+n2;
        n1=n2;
        n2=n3;
        cout<<n3<<endl;
    }
    return 0;
}

3. By Abhijeet

Made by Abhijeet. Fibonacci series program using constructor. Source

u entered the length: 12
fibonacci series upto 12 number is :
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89
#include <iostream>
using namespace std;
class fibo
{
public :int a=0,b=1,c,n;
        fibo()
       {
       cin>>n;
       cout<<"u entered the length: "<<n<<endl;
       cout<<"fibonacci series upto "<<n<<" number is :"<<endl;
       cout<<a<<" , "<<b;
       for(int i=2;i<n;i++)
       {
           c=a+b;
           cout<<" , "<<c;
           a=b;
           b=c;
       }
        }
};
int main() {
       fibo obj;
    return 0;
}

4. By ROHIT KANOJIYA

Made by ROHIT KANOJIYA. Source

Fibonacci Series:5
0 1 1 2 3 
#include <iostream>
using namespace std;
int fibo(int f1,int f2,int n);
int main() 
{
int f1=0,f2=1,n;
//cout<<"Enter how many Number you want to generate in a Series:";
 cin>>n;
 cout<<"Fibonacci Series:\n";
 cout<<f1<<" "<<f2<<" ";
 fibo(f1,f2,n-2);
  return 0;
}
int fibo(int f1,int f2,int n)   //RECURSIVE METHOD
{
 int f3;
  f3=f1+f2;
  cout<<f3<<" ";
  if(n>1)
   fibo(f2,f3,n-1);
}

5. By Rishabh

Made by Rishabh. C++ Fibonacci Series Program using Recursion. Source

10
1 1 2 3 5 8 13 21 34 55 
#include <iostream>
#if 0 //start 

    input_1: n (any no.);[Output will be 0 to nth term of fibonacci series],
    default value is 10 [if you leave input field empty]
    
#endif //end

void fib(int f,int l,int inp,int fi){
      f=f+l;
      l=f-l;
      std::cout<<f<<' ';
      if(inp-1>fi){
          fib(f,l,inp,fi+1);
      }
}
int main() {
    int x=10;
    std::cin>>x;
    fib(0,1,x,0);
    return 0;
}

6. By Bishu

Made by Bishu. Program that Prints the first 13 Fibonacci series numbers. Source

Fibonacci Series:0  1 1 2 3 5 8 13 21 34 55 89 144
#include <iostream>
using namespace std;

int main()
{
    int a, b, c, i, n;
    a = 0;
    b = 1;
    cout<<"Fibonacci Series:";
    cout<<a<<"  "<<b;
    for(i = 0; i <= 10; i++)
    {
        c = a + b;
        cout<<" "<<c;
        a = b;
        b = c;
    }
	return 0;
}

7. By Senpai

Made by Senpai. Source

Fibonacci series for 5 terms is: 1 1 2 3 5 
#include <iostream>
using namespace std;

int main() {

    int n, t1 = 0, t2 = 1, tn = 0;
    
    cin >> n;
    
    cout << "Fibonacci series for "<< n << " terms is: ";
    
        for (int i = 1; i<=n; i++) {
            
            //prints first two terms.
            if(i == 1)
            {
                cout << t2 << " ";
                continue;
            }
            tn = t1 + t2;
            t1 = t2;
            t2 = tn;
            
            cout << tn << " ";
        }
    
    return 0;
}

8. By Prabhu Nithin

Made by Prabhu Nithin. Program that prints first n numbers in Fibonacci series. Source

Enter a number to find first n fibonacci numbers:10
0 1 1 2 3 5 8 13 21 34
#include <iostream>
using namespace std;

int main() {
int a=0,b=1,c,n;
cout<<"Enter a number to find first n fibonacci numbers:";
cin>>n;
cout<<n<<endl;
c=a+b;
cout<<"0 1";
for (int i=1;i<n-1;i++)
  {
     cout<<" "<<c;
     a=b;
     b=c;
     c=a+b;
      
  }
    return 0;
}

9. By Storm

Made by storm. C++ program that prints the first 20 Numbers from Fibonacci Series. Source

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 
#include <iostream>
using namespace std;

int main() {

int a[20];
a[0]=0;
a[1]=1;


for (int i=2;i<20;i++){
    a[i]=a[i-1]+a[i-2];
    
};

for (int j=0;j<20;j++){
    cout<<a[j]<<" ";
};


    return 0;
}

10. By AliRaja

Made by AliRaja. Source

1st number:1
2nd number:1
3rd number:2
4th number:3
5th number:5
6th number:8
7th number:13
8th number:21
9th number:34
10th number:55
...
#include <iostream>
#define Ten_billion 10000000000
#define txt " number:"
using namespace std;
string counter(int i)
{   string num;
    if(i%10==1){num="st";}
    else if(i%10==2){num="nd";} 
    else if(i%10==3){num="rd";} 
    else{num="th";}
    return num;    }

int main()
{    unsigned long long first=1,second=1,sum;
    short int i=3;
cout<<"          created by AliReza\n1st"<<txt<<first<<"\n2nd"<<txt<<second<<endl;
    for (;i<94;i++){
    sum = first + second ;  
    first = second ; 
    second = sum ;
    cout<<i<<counter(i)<<txt<<sum<<endl; }
unsigned long long first10_digit1,first10_digit2,second10_digit1,second10_digit2,sum_first10_digit,sum_second10_digit,extra;
first10_digit1 = first % Ten_billion ;
second10_digit1 = first / Ten_billion ;
first10_digit2 = second % Ten_billion ;
second10_digit2 = second / Ten_billion ;
   for(;i<142;i++)
{     sum_first10_digit = first10_digit1 + first10_digit2 ;
    sum_second10_digit = second10_digit1 + second10_digit2 ;
    if(sum_first10_digit > Ten_billion) {
       extra = sum_first10_digit / Ten_billion;
       sum_first10_digit %= Ten_billion ;
       sum_second10_digit += extra ;}
    first10_digit1 = first10_digit2 ;
    first10_digit2 = sum_first10_digit ;
    second10_digit1 = second10_digit2 ;
    second10_digit2 = sum_second10_digit ;
    if (sum_first10_digit / (Ten_billion / 10) ==0) 
{ cout<<i<<counter(i)<<txt<<sum_second10_digit<<0<<sum_first10_digit<<endl;}   else{ cout<<i<<counter(i)<<txt<<sum_second10_digit<<sum_first10_digit<<endl;  }    }    }