Fibonacci Series Using Recursion In C/C++

Fibonacci Series Using Recursion In C/C++

A recursive function (recursion) is a function that calls itself inside its definition. The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. For e.g. 0 1 1 2 3 5 8 ….. is a fibonacci series. We will see complete explanation of fibonacci series using recursion in C/C++ in this post.

Fibonacci Series Using Recursion In C

Q. Write a program to print fibonacci series using recursion.

1. Program in C:

//Fibonacci series using recursive function
//Fibonacci series using recursive function
#include<stdio.h>
void fibo(int,int,int,int);  // Function declaration or prototype
int main()
{
    int num,n1=0,n2=1,count=2;
    printf("\n Enter limit for fibonacci series : ");
    scanf("%d",&num);
    printf("\n Fibonacci series of %d numbers is \n",num);
    printf("%d\t%d",n1,n2);
    fibo(num,n1,n2,count);   //Function calling
    return 0;
}
void fibo(int num,int n1,int n2,int count)  // Function definition
{
    int n3;
    if(count<num)
    {
        n3 = n1 + n2;
        n1 = n2;
        n2 = n3;
        count++;
        printf("\t%d",n3);
        fibo(num,n1,n2,count);   //Recursion or calling function again
    }
}

2. Program in C++:

//Fibonacci series using recursive function
#include<iostream>
using namespace std;
void fibo(int,int,int,int);  // Function declaration or prototype
int main()
{
    int num,n1=0,n2=1,count=2;
    cout<<"\n Enter limit for fibonacci series : ";
    cin>>num;
    cout<<"\n Fibonacci series of "<<num<<" numbers is \n";
    cout<<n1<<"\t"<<n2;
    fibo(num,n1,n2,count);   //Function calling
    return 0;
}
void fibo(int num,int n1,int n2,int count)  // Function definition
{
    int n3;
    if(count<num)
    {
        n3 = n1 + n2;
        n1 = n2;
        n2 = n3;
        count++;
        cout<<"\t"<<n3;
        fibo(num,n1,n2,count);   //Recursion or calling function again
    }
}

3. Output –

Fibonacci Series Using Recursion In C

4. Program Parts –

1.#include is a pre-processor directive. It is used to include header files.
2.stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3..h is an extension for header file.
4. void fibo(int, int, int, int) is function name. It is user defined function. It takes an integer value as parameter.
5.int is data type. It indicates that the program returns a value.
6.main() is main function that indicates the compiler that the user written programs starts from here.
7.num, n1, n2, count are variable names.
8.print/cout is the keyword use to print a message.
9.scanf/cin is the keyword use to store values in variables.
10. fibo(num, n1, n2, count) is calling function. It passes value n to function definition.
11.return statement is use to return a value. If any error occurs the program will return 0.

4. Explanation –

Here compiler checks for necessary header files. If all header files are present then compiler advances further. In next step we declare our function. Also known as function prototype. Here we are going to pass 4 integer values to function thus we write fibo(int.int,int,int).

Now the compiler enters main function. Here we declare four varibales namely num, n1, n2 and count. n1 and n2 are for first two numbers of fibonacci series and count is to prevent additional printing of numbers. Thus we initialize it to 2 as we print first two numbers i.e. n1 and n2. Now, compiler encounters fibo(num, n1, n2, count) i.e. function calling. From here compiler directly jumps to function definition.

Here in function definition we declare one variable n3 of integer type. This variable is to calculate preceeding number of fibonacci series. We check if the value of count is less than number of elements in fibonacci series that user wants. If yes then compiler advances further. Here we add n1 and n2 and store its value in n3. In next step we change values of n1 and n2 and print value of n3. Then we call our function again to print next values i.e. we make recursive call to our function. And once the condition in if statement is false, the compiler returns to tha main function. And then the program execution terminates after printing entire fibonacci series.

Conclusion-

So this was the program for fibonacci series in C/C++ using recursion. If you liked this post then do share it with your friends and programming buddies. If you have any doubts regarding this program do mention it in comment section below or mail us!

For daily updates follow us on Instagram. Do visit again!

ErrorFreeProgram.

Leave a Reply

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