C语言求Fibbonacci数列

来源:百度知道 编辑:UC知道 时间:2024/06/19 15:31:31
写递归函数求Fibbonacci数列的前40个数,并以每行5个数的格式print

int main()
{
int count, a=0, b=1;
for (count=0; count <40; ++count)
{
if (count % 5 == 0) printf("\n");
printf("%d\t", b);
b += a;
a = b-a;
}
}

#include <stdio.h>
#define ROW 8
#define COL 5

int main(int argc,char **argv){
int i,j,third,second,first;
first=0;
second=1;
for (i=0;i<ROW;i++){
for (j=0;j<COL;j++){
printf("%9d",first);
third=first+second;
first=second;
second=third;
}
printf("\n");
}
return 0;
}

Output:
0 1 1 2 3
5 8 13 21 34
55 89 144 233 377
610 987 1597 2584 4181
6765 10946 17711 28657 46368
75025 121393 196418 317811 514229
832040 13