用C语言输出斐波那契数列1、1、2、3、5、8、13、……前25项,要求每行输出5项
时间: 2023-12-28 07:24:13 浏览: 37
以下是C语言输出斐波那契数列的代码,每行输出5项:
```c
#include <stdio.h>
int main() {
int n = 25, i;
int f1 = 1, f2 = 1, f3;
printf("斐波那契数列的前%d项:\n", n);
for (i = 1; i <= n; i++) {
if (i == 1 || i == 2) {
printf("1 ");
} else {
f3 = f1 + f2;
printf("%d ", f3);
f1 = f2;
f2 = f3;
}
if (i % 5 == 0) {
printf("\n");
}
}
return 0;
}
```
运行结果:
```
斐波那契数列的前25项:
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
```
相关问题
C语言输出数列1 2 3 5 8 13 21……的前四十项
这个数列是斐波那契数列,可以使用循环结构和递推公式来输出前40项。以下是C语言代码示例:
```c
#include <stdio.h>
int main() {
int a = 1, b = 2, c, i;
printf("1 2 ");
for (i = 3; i <= 40; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
return 0;
}
```
输出结果为:
```
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 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437
```
编程用c语言求斐波那契数列的前25项:1 1 2 3 5 8……
好的,下面是用 C 语言求斐波那契数列前25项的代码:
```c
#include <stdio.h>
int main() {
int a = 1, b = 1, c;
printf("%d %d ", a, b);
for (int i = 2; i < 25; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
return 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
```
这个程序使用了三个变量 `a`、`b` 和 `c` 来保存斐波那契数列中当前要计算的三个数。在每次计算时,用 `c` 来保存当前计算出来的数,然后将 `a` 和 `b` 更新为下一个要计算的数。最后输出斐波那契数列的前25项。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)