Fibonacci Numbers in C++

Write a C++ program to write fibonacci numbers. You should input the number of fibonacci numbers you want in your program.

The fibonacci sequence is given by

$$F_i = \begin{cases} F_0 = 0, \\ F_1 = 1, \\ F_n = F_{n-1}+F_{n-2} \end{cases} $$

#include<iostream>
using namespace std;
int main(){
	int f0=0,f1=1,f2;
	int n;
	cout<<"Enter the numbers of integers "<<endl;
	cin>>n;
	cout<<n<<" numbers of fibonacci are "<<f0<<" "<<f1<<" ";
	while(n-2>0){
		f2 = f1 + f0;
		f0 = f1;
		f1 = f2;
		cout<<f2<<" ";
		n--;
	}
	return 0;
}
output of the above program.  fibonacci sequence
output of the above program.

Leave a Reply