Fibanacci Series in C++
What is Fibanacci Series?
A fibanacci series is a series where the next term is the series of last two series.
ex:- let the first two terms of fibanacci series would be 0 and 1 , and the next term will be 1, because sum of last terms is (1 + 0 = 1) , and the next term will ne (1+1=0).
Fibanacci Series up to 10 : ( 0, 1, 1, 2, 3, 5, 8).
Programm to print n number of fibanacci series.
#include <iostream>
using namespace std;
int main(){
int n, a = 0, b = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
// Prints the first two terms.
if(i == 1) {
cout << a << ", ";
continue;
}
if(i == 2) {
cout << b << ", ";
continue;
}
nextTerm = a + b;
a = b;
b = nextTerm;
cout << nextTerm << ", ";
return 0;
}
output will be