while输出斐波那契数列前20项
时间: 2023-11-18 12:59:50 浏览: 105
下面是使用while语句输出斐波那契数列前20项的代码:
```c++
#include <iostream>
using namespace std;
int main() {
int n1 = 0, n2 = 1, n3, i = 2;
cout << n1 << " " << n2 << " ";
while (i < 20) {
n3 = n1 + n2;
cout << n3 << " ";
n1 = n2;
n2 = n3;
i++;
}
return 0;
}
```
输出结果:
```
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
```
相关问题
用while写斐波那契数列前20项
```
a, b = 0, 1
count = 0
while count < 20:
print(b)
a, b = b, a + b
count += 1
```
输出:
```
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
```
Python如何用while语句输出斐波那契数列的前20项
以下是Python用while语句输出斐波那契数列的前20项的代码:
```
a, b = 0, 1
count = 0
while count < 20:
print(b)
a, b = b, a + b
count += 1
```
该段代码输出的结果如下:
```
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
```
阅读全文