C++ Program to generate fibonacci series
Program
#include<iostream>
using namespace std;
int main()
{
int range;
cout << "Enter the range of fibonacci serics" << endl;
cin >> range;
if(range == 0)
{
cout << "Cannot generate series" << endl;
exit(0);
}
int term_one = 0;
int term_two = 1;
cout << "Fibonacci Series" << endl;
if(range == 1)
cout << term_one;
else if(range >= 2)
{
cout << term_one << "\t" << term_two;
for(int i = 0; i < range - 2; i++)
{
int fib = term_one + term_two;
cout << "\t" << fib;
term_one = term_two;
term_two = fib;
}
}
cout << endl;
return 0;
}
Output 1
$ g++ fibonacci.cpp
$ ./a.out
Enter the range of fibonacci serics
6
Fibonacci Series
0 1 1 2 3 5
Output 2
$ ./a.out
Enter the range of fibonacci serics
0
Cannot generate series