C++ Program for Fibonacci Series

A series in which each number is the sum of the preceding two numbers is called the Fibonacci series.

For example 0 1 1 2 3 5 8 13 . . . . .

Below is the program to find Fibonacci series in C++.

#include<iostream>

using namespace std;

int main()
{
	long n,first=0,second=1,third;
	cout<<"How many numbers?";
	cin>>n;
	cout<<"Fibonacci series\n"<<first<<" "<<second;

	for(int i=2;i<n;++i)
	{
		third=first+second;
		cout<<" "<<third;
		first=second;
		second=third;
	}

	return 0;
}

Output:

How many numbers?5
Fibonacci series
0 1 1 2 3

2 thoughts on “C++ Program for Fibonacci Series”

Leave a Comment

Your email address will not be published. Required fields are marked *