The Fibonacci sequence is a series where the next term is the sum of previous two terms. The first two terms of the Fibonacci sequence is 0 followed by 1.
For Example: 0,1,1,2,3,5,8,13,21 is a Fibonacci Series of length 9.
C++ Program to Display Fibonacci Series
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of Terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
if (i == 1) {
cout << " " << t1 << " ";
continue;
}
if (i == 2) {
cout << "," << t2 << " ,";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
return 0;
}
Output of Program
Enter the number of Terms: 20
Fibonacci Series: 0 ,1 ,1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181,