codescracker
c++

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:

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:

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:

// C++ Program to Print Fibonacci Series
// ----codescracker.com----

#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:

C++ program print Fibonacci series

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:

fibonacci series in c++

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:

// C++ Print Fibonacci Series upto Given Number
// ----codescracker.com----

#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 program in c++

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.

// Fibonacci Series using Function
// ----codescracker.com----

#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";
        cout<<"Enter Your Choice: ";
        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:

print fibonacci series in c++

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:

fibonacci series using function c++

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:

fibonacci series program using function c++

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:

c++ print fibonacci series

Same Program in Other Languages


« Previous Program Next Program »