Implement functions to print nth Fibonacci number using iteration and recursive method.
Implement functions to print nth Fibonacci number using iteration and recursive method. Compare the performance of two methods by counting number of steps executed on various inputs. Also draw a comparative chart. (Fibonacci series 1, 1, 2, 3, 5, 8….. Here 8 is the 6th Fibonacci number)
#include<stdio.h>
#include<conio.h>
int iteration(int);
int recursive(int);
int count=0;
void main()
{
int n,choice,ans;
clrscr();
printf("\nenter nth number :
");
scanf("%d",&n);
printf(“\n==iteration==”);
ans=iteration(n);
printf(“\nans=%d\ncount=%d”,ans,count);
printf(“\n\n==recursive==”);
ans=recursive(n);
printf("\nans=%d\ncount=%d",ans,count);
getch();
}
int iteration(int n)
{
int f=1,s=1,t,i;
count++;
count++;
for(i=3;i<=n;i++)
{
t=f+s;
count++;
f=s;
count++;
s=t;
count++;
}
count++;
return t;
}
int recursive(int n)
{
count++;
if(n==0 || n==1)
{
count++;
return n;
}
else
{
count++;
return recursive(n-1) +
recursive(n-2);
}
Comments
Post a Comment