C PROGRAM FOR FIBONACCI SERIES USING RECURSION
Consider the following I/O samples.
SAMPLE INPUT: 5
SAMPLE OUTPUT: 0 1 1 2 3
The algorithm used here is :
- Input n .
- Call the function fibo(int n)
- In the function perform
i. if(n==0 || n==1)return n
ii. return(fibo(n-1) + fibo(n-2))
4. Print the output.
C PROGRAM:
#include<stdio.h>
int fibo(int);
int main()
{
int n, m= 0, i;
printf("Enter the number of elements:");
scanf("%d", &n);
printf("Fibonacci series terms are:\n");
for(i = 1; i <= n; i++)
{
printf("%d ", fibo(m));
m++;
}
return 0;
}
int fibo(int n)
{
if(n == 0 || n == 1)
return n;
return(fibo(n-1) + fibo(n-2));
}
OUTPUT:
The given source code in C program "for Fibonacci series using recursion" is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.
If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment