C program to find Fibonacci Series Using Recursion

1.Write a C program to find the nth item of fibonacci number.

 #include <stdio.h>

int fibonacci(int n) {

   if (n==0) {

        return 0;

    }

else if(n==1){

       return 1;

}

else {

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

}

}

int main() {

    int n;

    printf("Enter a positive integer: ");

    scanf("%d", &n);

    printf("The %dth Fibonacci number is %d\n", n, fibonacci(n));

    return 0;

}

Alternative ways:

#include <stdio.h>

int main() {

int i,n,a=o,b=1,c;

Printf("Enter number of terms: \n"); 

scanf ("%d", &n);

printf("Fibonacci sequence:%d %d", a,b);

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

    { 

        c=a+b;

        Printf("%d\n",c);

        a=b;

        b=c;

    }

return 0;

}

Output :

If the given nth term is 6 then,

0 1 1 2 3 5


Post a Comment

0 Comments