Updated March 17, 2023
Introduction to Fibonacci Series in C++
Fibonacci series is a series of numbers. It makes the chain of numbers adding the last two numbers. Calculating the Fibonacci series is easy as we have to just add the last two-digit to get another digit. But at some point when the number of digits becomes larges, it quite becomes complex. So to solve this problem, we are going to see how we can create a Fibonacci series program in the C++ language so that it will become easy.
Let us see how the Fibonacci series actually works. Let f(n) be the nth term.
- f(0)=0;
- f(1)=1;
Series Will Be as Follows:
- 1 (1+0)
- 2 (1+1)
- 3 (1+2)
- 5 (2+3)
- 8 (3+5)
- 13 (5+8)
- 21 (8+13
…and so on
Logic Behind Generating Fibonacci Series
- Initialize the first number to 0
- Initialize the second number to 1
- Add the first and second numbers.
- Store the value of adding in the third number.
- Print the third number.
- Assign the second number to the first number
- Assange the third number to the second number.
- Repeat step 3 to step 7 until the Fibonacci series for a given number is calculated.
Fibonacci Series Programs in C++
In this section, we are going to see how to find out the Fibonacci series using various methods like recursion, loop, array, without recursion, etc:
Example #1 – Without the Recursion Method
Code:
#include<iostream>
using namespace std;
int main()
{
int first_num = 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" <<endl;
cout << first_num <<endl;
cout << second_num << endl;
for(i = 2; i < num; ++i) //loop will starts from 2 because we have printed 0 and 1 before
{
third_num = first_num + second_num;
cout << third_num << endl;
first_num = second_num;
second_num = third_num;
}
return 0;
}
Output:
Example #2 – By Using Recursion Method
Code:
#include<iostream>
using namespace std;
void FibonacciSeries(int num)
{
static int first_num = 0, second_num = 1, third_num;
if(num > 0)
{
third_num = first_num + second_num;
first_num = second_num;
second_num = third_num;
cout << third_num << endl;
FibonacciSeries(num - 1);
}
}
int main()
{
int num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number: \n" << endl;
cout << "0" << endl;
cout << "1" << endl;
FibonacciSeries (num - 2); //number-2 is used because we have already print 2 numbers
return 0;
}
Output:
Example #3 – Using a For loop
Code:
#include<iostream>
using namespace std;
int main()
{
int first_num= 0, second_num = 1, third_num, i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
cout << "Fibonacci Series for a given number:" <<endl;
cout << first_num <<endl;
cout << second_num <<endl;
for(i = 0; i < num; i++) //loop will starts from 2 because we have printed 0 and 1 before
{
if(i <= 1)
third_num = i;
else
{
third_num = first_num + second_num;
cout << third_num <<endl;
first_num = second_num;
second_num = third_num;
}
}
return 0;
}
Output:
Example #4 – By Using While Loop
Code:
#include<iostream>
using namespace std;
int main()
{
int first_num = 0, second_num = 1, third_num = 0, i = 3, num;
cout << "Enter random number to print fibonacci series:" ;
cin >> num;
cout << "Fibonacci Series for a given number:" <<endl;
cout << first_num <<endl;
cout << second_num <<endl;
while(i <= num)
{
third_num = first_num + second_num;
cout << third_num <<endl;
first_num = second_num;
second_num = third_num;
i = i + 1;
}
return 0;
}
Output:
Example #5 – Using an array
Code:
#include<iostream>
using namespace std;
int main()
{
int fibonacci[25], i, num;
cout << "Enter random number to print fibonacci series:";
cin >> num;
fibonacci[0] = 0;
fibonacci[1] = 1;
for (i = 2; i < num; i++)
{
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
cout <<"Fibonacci Series for a given number:" << endl;
for (i = 0; i < num; i++)
{
cout << fibonacci[i] <<endl;
}
return 0;
}
Output:
Example #6 – Using specified number
Code:
#include<iostream>
using namespace std;
int main()
{
int first_num = 0, second_num = 1, third_num, i;
cout << "Fibonacci Series for a given number:";
cout << first_num <<endl;
cout << second_num <<endl;
for(i = 2; i < 10; ++i) //loop will starts from 2 because we have printed 0 and 1 before
{
third_num = first_num + second_num;
cout << third_num <<endl;
first_num = second_num;
second_num = third_num;
}
return 0;
}
Output:
Conclusion
In this article, we have seen how to generate the Fibonacci series using various methods. I hope you’ll find this article helpful.
Recommended Articles
This is a guide to Fibonacci Series in C++. Here we discuss the different programs of the Fibonacci Series in C++. You can also go through our other related articles to learn more-