Fibonacci使用递归和循环实现

时间:2021-03-05 07:36:13
#include<stdio.h>

double Fibonacci(int i);
double Fibonacci_(int i); int main(void)
{
int i;
printf("Enter the number i:\n");
scanf("%d", &i); printf("%f %f",Fibonacci(i),Fibonacci_(i));
return 0;
}
double Fibonacci(int i)
{
double k=1;
double g;
for(double n=0;i-1>=0;i--)
{
g=k+n;
n=k;
k=g;
}
return g;
}
double Fibonacci_(int i)
{
double k;
if(i==2) return 2;
else if(i==1) return 1;
else if(i<=0) return 0;
else
k=Fibonacci_(i-1)+Fibonacci_(i-2);
return k;
}