written 5.7 years ago by | • modified 5.7 years ago |
Write a program to print Fibonacci series up to N terms using a recursive function.
Subject : Structured Programming Approach
Title : Functions and Parameter
Difficulty : Medium
written 5.7 years ago by | • modified 5.7 years ago |
Write a program to print Fibonacci series up to N terms using a recursive function.
Subject : Structured Programming Approach
Title : Functions and Parameter
Difficulty : Medium
written 5.7 years ago by | • modified 5.7 years ago |
Recursion:
A function that calls itself is called as recursive function and this technique is called as recursion.
Advantages:
Reduce unnecessary calling of functions.
Through Recursion one can solve problems in easy way while its iterative solution is very big and complex.
Extremely useful when applying the same solution.
Disadvantages:
Recursive solution is always logical and it is very difficult to trace.
In recursive we must have an if statement somewhere to force the function to return without the recursive call being executed, otherwise the function will never return.
Recursion uses more processor time
i. A recursive function must definitely have a condition that exits from calling a function again.
ii. Hence there must be condition that calls the function itselfif that condition is true.
iii. If the condition is false then it will exit from the loopof calling itself again.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
int fibo(int,int,int);
clrscr();
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:
Enter the number of elements:10
0
1
1
2
3
5
8
13
21
34