Home >>Cpp Programs >Fibonacci Series in C++

Fibonacci Series in C++

Fibonacci Series in C++

Fibonacci Series in C ++ is generally a recurrence sequence in which the next number is the sum of following two numbers, for instance 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. Please note that the first two numbers of Fibonacci series are always 0 and 1.

We can the Fibonacci series program in C++ in two ways that are depicted below:

  • Fibonacci Series without recursion
  • Fibonacci Series using recursion

1. Fibonacci Series in C++ without Recursion

Here is an example of the Fibonacci series in C++ that is without the recursion that will help you in understanding the topic from a greater depth:

#include <iostream>  
using namespace std;  
int main() 
{  
  int num1=0,num2=1,num3,i,number;    
 cout<<"Please Enter the number of elements you want to print : ";    
 cin>>number;    
 cout<<num1<<" "<<num2<<" "; //First print 0 and 1    
 for(i=2;i<number;++i)    
 {    
  num3=num1+num2;    
  cout<<num3<<" ";    
  num1=num2;    
  num2=num3;    
 }    
   return 0;  
   }  
Output : Please Enter the number of elements you want to print : 5
0 1 1 2 3

2. Fibonacci series using recursion in C++

Here is an example of the Fibonacci series in C++ that is with the use of recursion that will help you in understanding the topic from a greater depth:

#include<iostream>    
using namespace std;      
void Fibonacci(int n)
{    
    static int num1=0, num2=1, num3;    
    if(n>0)
	{    
         num3 = num1 + num2;    
         num1 = num2;    
         num2 = num3;    
         cout<<num3<<" ";    
         Fibonacci(n-1);    
    }    
}    
int main()
{    
    int n;    
    cout<<"Please Enter the number of elements you want to print : ";    
    cin>>n;    
    cout<<"Series are : ";    
    cout<<"0 "<<"1 ";  
    Fibonacci(n-2);   
     return 0;  
} 
Output : Please Enter the number of elements you want to print : 5
0 1 1 2 3

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++