Thursday, 26 January 2012

Write a C program to generate the Fibonocci series using Recursion.

#include<stdio.h>
#include<conio.h>
int fibno(int); // function declaration.
void main()
{
    int ct, n, disp;
    clrscr( );
    printf("Enter the no. of terms:");
    scanf("%d", &n);
    printf("\n The Fibonocci series:\n");
    for( ct=0; ct<=n-1; ct++)
    {
        disp= fibno(ct); //calling function.
        printf("%5d", disp);
    }
    getch( );
}

int fibno( int n)
{
    int x, y;
    if(n==0)
    return 0;
    else if(n==1)
    return 1;
    else
    {
        x= fibno( n-1);
        y= fibno( n-2);
        return (x+y);
    }
}


Output:- Enter the no. of terms: 10

The Fibonocci series:
0 1 1 2 3 5 8 13 21 34

No comments:

Post a Comment