C PROGRAM FOR FIBONACCI SERIES USING RECURSION

  

Consider the following I/O samples.


SAMPLE INPUT: 5

SAMPLE OUTPUT: 0 1 1 2 3


The algorithm used here is :

    1. Input n .
    2. Call the function fibo(int n)
    3. In the function perform
            i. if(n==0 || n==1)

                    return n

            ii. return(fibo(n-1) + fibo(n-2))

      4Print the output.


C PROGRAM:  

#include<stdio.h>

int fibo(int);

int main()

{

    int n, m= 0, i;

    printf("Enter the number of elements:");

        scanf("%d", &n);

    printf("Fibonacci series terms are:\n");

    for(i = 1; i <= n; i++)

    {

        printf("%d ", fibo(m));

        m++;

    }

return 0;

}

int fibo(int n)

{

    if(n == 0 || n == 1)

        return n;

    return(fibo(n-1) + fibo(n-2));

}


OUTPUT:





The given source code in C program "for Fibonacci series using recursion" is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free. 

If you have any feedback about this article and want to improve this, please comment in the comment section.


Comments

Popular posts from this blog

INVERTED PYRAMID STAR PATTERN

C PROGRAM TO PRINT ALL LEAP YEARS IN A GIVEN RANGE

Design a way to sort the list of positive integers in the descending order according to frequency of elements. The elements with integers with higher frequency come before with lower frequency elements with same frequency come in the same order as they appear the values.