C++ Program to Print Fibonacci series using While Loop

C++ Program to Print Fibonacci series using While Loop

Write C++ Program to Print Fibonacci series using While Loop

// CPP Program to Print Fibonacci series using While Loop

#include <iostream>

using namespace std;

int main()
{
	int n, i, f1, f2, f3;
	cout << "Enter the number :--> ";
	cin >> n;

	i = 1;
	f1 = 0;
	f2 = 1;
	cout << f1 << " " << f2 << " ";
	while(i < n)
	{
		f3 = f1 + f2;
		cout << f3 << " " ;
		f1 = f2;
		f2 = f3;
		i++;
	}
	return 0;
}

Output:

Enter the number :--> 5
0 1 1 2 3 5