Write a C Program to display Fibonacci series using recursion

Write a C Program to display Fibonacci series using recursion

#include<stdio.h>
int fibonacci_series(int);
int main()
{
int count, c = 0, i;
printf(“Enter number of terms:”);
scanf(“%d”,&count);

printf(“\nFibonacci series:\n”);
for ( i = 1 ; i <= count ; i++ )
{
printf(“%d\n”, fibonacci_series(c));
c++;
}

return 0;
}
int fibonacci_series(int num)
{
if ( num == 0 )
return 0;
else if ( num == 1 )
return 1;
else
return ( fibonacci_series(num-1) + fibonacci_series(num-2) );
}

(Or)

#include<stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}

int main ()
{
int n = 9;
printf(“%d”, fib(n));
getchar();
return 0;
}

Program 1:

#include <stdio.h>
int main()
{
int i, n, a = 0, b = 1, c;
printf(“\nEnter the number of terms: “);
scanf(“%d”, &n);
printf(“\nFibonacci series of given number: “);
for (i = 1; i <= n; ++i)
{
printf(“%d, “, a);
c = a + b;
a = b;
b = c;
}
return 0;
}

Output:

Enter the number of terms: 7
Fibonacci series of given number: 0, 1, 1, 2, 3, 5, 8,

No Comments

Post a Reply