0
1.9kviews
Explain recursion concept. Write a program to display Fibonacci series using recursion.
1 Answer
written 8.3 years ago by |
Recursive Function:
A function that calls itself is called as recursive function.
A recursive function must definitely have a condition that exists from calling the function again.
Hence there must be a condition that calls the function itself if that condition is true.
If the condition is false then it will exit from the loop of calling itself again.
Algorithm:
Main () Function:
Fibo(parameters : a, b, i)
Program:
#include <stdio.h>
#include <conio.h>
int main(){
int n, i;
int fibo (int, int, int);
printf("Enter the number of Elements:");
scanf("%d", &n);
printf("0\n");
for(i=1; i<n-1; i++) {
printf("%d\n", fibo(0, 1, i));
}
getch();
}
int fibo(int a, int b, int i){
if(i==1)
return b;
else
return (fibo(b, a+b, --i));
}
Output:
How Many Numbers: 10
0
1
1
2
3
5
8
13
21
34