Tuesday, November 15, 2011

Find fibonacci number at position n: recursion

#include <stdio.h>

int fib(int n)
{
    if(n<=1)
        return n;
    return fib(n-1)+fib(n-2);
}


int main()
{
    int a,r;
    printf("Enter any number : ");
    scanf("%d",&a);
    r=fib(a-1);
    printf("The no. at position %d is %d\n",a,r);
    return 0;
}


No comments: