c语言编程斐波那契数列 
时间: 2023-05-21 15:05:40 浏览: 16
以下是C语言实现斐波那契数列的代码:
```
#include<stdio.h>
int main()
{
int n, i;
long long arr[100];
arr[0] = 0;
arr[1] = 1;
printf("请输入需要输出的斐波那契数列项数:\n");
scanf("%d", &n);
for (i = 2; i <= n; i++)
{
arr[i] = arr[i - 1] + arr[i - 2];
}
printf("斐波那契数列如下:\n");
for (i = 0; i <= n; i++)
{
printf("%lld ", arr[i]);
}
return 0;
}
```
只要输入需要输出的斐波那契数列项数即可得到对应的斐波那契数列。
相关问题
c语言 编程斐波那契数列前40项(要求每行输出4项)
Sure, here's the code for generating the first 40 terms of the Fibonacci sequence using C language:
```c
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("The first 40 terms of the Fibonacci sequence are:\n");
for(n = 0; n < 40; n++) {
if(n % 4 == 0 && n > 0)
printf("\n"); // start a new line every 4 terms
if(n <= 1)
next = n;
else {
next = first + second;
first = second;
second = next;
}
printf("%d\t", next); // output the next term
}
return 0;
}
```
I hope that helps! Let me know if you have any other questions.
C语言编程斐波那契数列的前几项是:1、1、2、3、5、8、13、21……编程输出该数列的前20项,每行输出4个。
好的,下面是使用C语言编写的代码,用于输出斐波那契数列的前20项,每行输出4个数字:
```c
#include <stdio.h>
int main() {
int n = 20; // 总共输出的项数
int count = 0; // 当前已输出的项数
int a = 1, b = 1; // 前两项
while (count < n) {
printf("%d ", a);
count++;
if (count % 4 == 0) {
printf("\n"); // 每行输出4个数字后换行
}
int temp = a;
a = b;
b = temp + b;
}
return 0;
}
```
这段代码使用了循环来生成斐波那契数列,并在每行输出4个数字后换行。输出结果如下:
```
1 1 2 3
5 8 13 21
34 55 89 144
233 377 610 987
1597 2584 4181 6765
```
相关推荐














