Recursion: Fibonacci Series
Write a program to print N Fibonacci terms using Recursion.
#include<stdio.h>
int fib(int n);
void main()
{
int
i, n;
printf("\nEnter
the value of n:");
scanf("%d",
&n);
printf("\n
%d fibonacci terms are: ", n);
for(i=0;
i<n; i++)
printf("%d
", fib(i));
}
int fib(int n)
{
if(n
== 0)
return
0;
else
if(n == 1)
return
1;
else
return
(fib(n-1) + fib(n-2));
}
Output:
Enter the value of
n: 10
10 fibonacci terms
are:
0 1
1 2 3 5 8
13 21 34
No comments:
Post a Comment