fibonacci数列c++
时间: 2023-06-29 19:09:17 浏览: 128
class3-Fibonacci.rar_Fibonacci_Fibonacci+c++_fibonacci数列
下面是一个简单的 C++ 程序,用于生成斐波那契数列:
```c++
#include <iostream>
using namespace std;
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i)
{
// Prints the first two terms.
if(i == 1)
{
cout << t1 << " ";
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << " ";
}
return 0;
}
```
这个程序要求用户输入要生成的斐波那契数列的项数,并使用循环结构和条件语句来生成和输出斐波那契数列。
阅读全文