1、数组打印斐波那契数列c++
时间: 2023-11-14 17:07:17 浏览: 38
Sure, I can help you with that! Here's some code to print the Fibonacci sequence in an array using C:
```
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of terms in the Fibonacci sequence: ");
scanf("%d", &n);
int fib[n];
fib[0] = 0;
fib[1] = 1;
for(i = 2; i < n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
printf("The Fibonacci sequence is: ");
for(i = 0; i < n; i++) {
printf("%d ", fib[i]);
}
printf("\n");
return 0;
}
```
I hope that helps!
相关问题
数组打印斐波那契数列c++
好的,这是一个关于编程的问题。以下是用 C 语言打印斐波那契数列的代码:
```c
#include <stdio.h>
int main() {
int n, i;
int fib[100];
printf("请输入要打印的斐波那契数列的项数:\n");
scanf("%d", &n);
fib[0] = 0;
fib[1] = 1;
for (i = 2; i < n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
printf("斐波那契数列的前 %d 项为:\n", n);
for (i = 0; i < n; i++) {
printf("%d ", fib[i]);
}
printf("\n");
return 0;
}
```
希望能够帮到您!
用c++的数组求斐波那契数列的第n项
以下是用 C 语言的数组求斐波那契数列的第 n 项的代码示例:
```c
#include <stdio.h>
int main() {
int n, i;
printf("请输入要求的斐波那契数列的项数:\n");
scanf("%d", &n);
int fib[n];
fib[0] = 0;
fib[1] = 1;
for (i = 2; i <= n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
printf("斐波那契数列的第 %d 项为:%d\n", n, fib[n]);
return 0;
}
```
该程序中,我们先输入要求的斐波那契数列的项数,然后声明一个长度为 n 的数组 fib,用来存储斐波那契数列的前 n 项。数组中的第一项为 0,第二项为 1,之后通过循环计算得到每一项的值。最后输出斐波那契数列的第 n 项的值。