斐波那契数列非递归 c
时间: 2023-11-15 10:55:23 浏览: 56
C语言实现斐波那契数列(非递归)的实例讲解
5星 · 资源好评率100%
斐波那契数列非递归 C 代码如下:
```c
#include <stdio.h>
int fibonacci(int n) {
if (n == 0 || n == 1) {
return n;
}
int a = 0, b = 1, c;
for (int i = 2; i <= n; i++) {
c = a + b; a = b;
b = c;
}
return b;
}
int main() {
int n = 10;
printf("斐波那契数列第%d项为%d\n", n, fibonacci(n));
return 0;
}
```
阅读全文