C++ Program to Print Fibonacci Series
Here you will get an example code of C++ program to print Fibonacci series.
The Fibonacci series is a list of numbers in which, each number is the sum of the two preceding ones, usually starting with 0 and 1. So, the sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth. Mathematically, it can be defined by the recurrence relation:
F(n)=F(n−1)+F(n−2)
with initial conditions:
F(0)=0, F(1)=1
Example C++ Program to Print Fibonacci series
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; int main() { int a=0,b=1,c,i,n; cout<<"Enter the number of terms:"; cin>>n; cout<<"Fibonacci Series: "<<a<<" "<<b; for(i=2;i<n;i++) { c=a+b; a=b; b=c; cout<<" "<<c; } return 0; } |
Output
Enter the number of terms:12
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89
Check out our other C++ programming Examples