c++ 打印斐波那契数列的前20项,要求每行打印5个。
时间: 2023-11-17 10:08:34 浏览: 39
Sure, here's the code to print the first 20 numbers of the Fibonacci sequence with 5 numbers per line:
```c
#include <stdio.h>
int main() {
int a = 0, b = 1, c, count = 0;
printf("Fibonacci series:\n");
for(int i = 1; i <= 20; i++) {
c = a + b;
a = b;
b = c;
printf("%d ", c);
count++;
if(count % 5 == 0) {
printf("\n");
}
}
return 0;
}
```
Hope that helps! And now for the joke: Why did the tomato turn red? Because it saw the salad dressing!
相关问题
c++输出fibonacci数列前20项每行输出5个代码
#include <stdio.h>
int main() {
int n = 20;
int a = 0, b = 1;
int i, count = 0;
for (i = 1; i <= n; i++) {
printf("%d ", a);
count++;
if (count == 5) {
printf("\n");
count = 0;
}
int temp = b;
b = a + b;
a = temp;
}
return 0;
}
用c++语言输出斐波那契数列前20项并且每行输出5
以下是用C++语言输出斐波那契数列前20项并且每行输出5的代码:
```c++
#include <iostream>
using namespace std;
int main()
{
int fib[20];
fib[0] = 0;
fib[1] = 1;
cout << "斐波那契数列前20项为:" << endl;
for (int i = 2; i < 20; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
for (int i = 0; i < 20; i++)
{
cout << fib[i] << " ";
if ((i+1) % 5 == 0)
{
cout << endl;
}
}
return 0;
}
```
输出结果为:
```
斐波那契数列前20项为:
0 1 1 2 3
5 8 13 21 34
55 89 144 233 377
610 987 1597 2584 4181
```