# C++ Program to Print Fibonacci Series

« Previous Program Next Program »

In this article, you will learn and get code to print Fibonacci series using C++ program in following ways:

• Print Fibonacci Series of N Terms
• Print Fibonacci Series upto Given Number
• Print Fibonacci Series using Function. This program includes both ways of printing Fibonacci series

In first program, if user enters a number say 10 as input, then the program prints Fibonacci series with 10 terms like:

`0  1  1  2  3  5  8  13  21  34`

Whereas in second program, if user enters 10 as input, then the program prints Fibonacci series in which the last number can not be greater than 10. Here is the Fibonacci series upto 10:

`0  1  1  2  3  5  8`

In third program, we've implemented both features using switch case and user-defined functions.

But before going through these programs, let's understand about Fibonacci series.

### What are Fibonacci Series ?

Fibonacci series is a sequence of numbers in which the first two numbers starts with 0 and 1. And all the upcoming numbers gets calculated as the summation of its previous two numbers in following ways:

• 0 as first number
• 1 as second number
• 1 as third number. Third number is the summation of second (1) and first (0) number. That is, 1+0
• 2 as fourth number. Fourth number is the summation of third (1) and second (1) number. That is, 1+1
• 3 as fifth number. That is, 2+1
• 5 as sixth number. That is, 3+2
• and so on

## Print Fibonacci Series of N Terms

To print Fibonacci series of N terms in C++ programming, you have to ask from user to enter the size of Fibonacci sequence or series. And then generate and print Fibonacci series of given size as shown in the program given below:

```#include<iostream>
using namespace std;
int main()
{
int a=0, b=1, c, tot, temp, i;
cout<<"Enter the Size of Fibonacci Sequence: ";
cin>>tot;
cout<<"\nFibonacci Series of "<<tot<<" Terms:\n";
for(i=1; i<=tot; i++)
{
if(i==1)
c = 0;
else if(i==2 || i==3)
c = 1;
else
{
temp = a;
a = b;
b = c;
c = a+b;
}
if(i==tot)
cout<<c;
else
cout<<c<<", ";
}
cout<<endl;
return 0;
}```

This program was build and run under Code::Blocks IDE. Here is its sample run:

Now supply the input as number to print Fibonacci series. Here the number indicates number of digits or terms (whatever you say), Fibonacci series contains. For example, if user enters 10 as input, then the program prints Fibonacci series that contains 10 digits or numbers. Here is the sample output with user input, 10:

## Print Fibonacci Series upto Given Number

This program generates Fibonacci series in a way that the last number can't be greater than the number entered by user as shown in the program given below.

The question is, write a program in C++ to print Fibonacci series upto given number. Here is its answer:

```#include<iostream>
using namespace std;
int main()
{
int a=0, b=1, c, limit, temp, i;
cout<<"Enter the Limit: ";
cin>>limit;
cout<<"\nFibonacci Series upto "<<limit<<":\n";
for(i=1; ; i++)
{
if(i==1)
c = 0;
else if(i==2 || i==3)
c = 1;
else
{
temp = a;
a = b;
b = c;
c = a+b;
}
if(c>limit)
break;
cout<<c<<"  ";
}
cout<<endl;
return 0;
}```

Here is its sample run with user input, 200:

## Fibonacci Series using Function

This program generates and prints Fibonacci series of N term and upto given number both, whatever user wants to perform using menu-driven feature. This program uses user-defined functions namely FiboOfNTerm() and FiboUptoGivenNumber() to print Fibonacci series in both ways.

```#include<iostream>
using namespace std;
void FiboOfNTerm(int);
void FiboUptoGivenNumber(int);
int main()
{
int ch, N, limit;
do
{
cout<<"1. Fibonacci Series of N Term\n";
cout<<"2. Fibonacci Series upto Given Number\n";
cout<<"3. Exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the Value of N: ";
cin>>N;
FiboOfNTerm(N);
break;
case 2:
cout<<"\nEnter the Number (Limit): ";
cin>>limit;
FiboUptoGivenNumber(limit);
break;
case 3:
return 0;
default:
cout<<"\nWrong Input!";
break;
}
cout<<"\n\n";
}while(ch==1 || ch==2);
cout<<endl;
return 0;
}
void FiboOfNTerm(int tot)
{
int i, a=0, b=1, c, temp;
for(i=1; i<=tot; i++)
{
if(i==1)
c = 0;
else if(i==2 || i==3)
c = 1;
else
{
temp = a;
a = b;
b = c;
c = a+b;
}
cout<<c<<"  ";
}
}
void FiboUptoGivenNumber(int limit)
{
int i, a=0, b=1, c, temp;
for(i=1; ; i++)
{
if(i==1)
c = 0;
else if(i==2 || i==3)
c = 1;
else
{
temp = a;
a = b;
b = c;
c = a+b;
}
if(c>limit)
break;
cout<<c<<"  ";
}
}```

Here is its sample run. This is the initial output:

Now enter your choice, that is, in what way you want to print Fibonacci series. Here is the sample output produced after entering 1 as choice and pressing `ENTER` key:

Enter the value of N to generate and print Fibonacci series of N numbers. Here is its sample output after supplying or entering 10 as value of N:

As you can see that the Fibonacci series of 10 terms gets printed and the choice again gets displayed to continue the operation. Let's check it out with second and then third option as shown in the snapshot given below:

#### Same Program in Other Languages

C++ Online Test

« Previous Program Next Program »