斐波那契数列:0、1、1、2、3、5、8、13、21
Fibonacci Series up to n number of terms
C++
xxxxxxxxxx
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;
}
Enter the number of terms: 10 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Program to Generate Fibonacci Sequence Up to a Certain Number
C++
xxxxxxxxxx
using namespace std;
int main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;
cout << "Enter a positive number: ";
cin >> n;
// displays the first two terms which is always 0 and 1
cout << "Fibonacci Series: " << t1 << ", " << t2 << ", ";
nextTerm = t1 + t2;
while(nextTerm <= n) {
cout << nextTerm << ", ";
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Enter a positive integer: 100 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
Linux - 如何安装 anc-api-tools
Linux - 如何安装 curvedns
Python - 检查数字是正数、负数还是 0
Linux - 如何安装 zenmap
C - 计算整数中的位数
C++ - 检查闰年
JavaScript - 检查字符串是否以某些字符开头和结尾
C++ - 检查一个数字是否是素数
C - 使用动态内存分配查找最大数
Kotlin - 反转数字
Kotlin - 将数字四舍五入到 n 个小数位
Linux - 如何安装 xbitmaps
Linux - 如何安装 zstd
Java - 反转数字
Python - 编写 Excel (XLSX) 文件
C++ - 求商和余数
Linux - 如何安装 zfs-test
Linux - 如何安装 zookeeper-bin
We have been online since 2021 and 1 millions of people around the globe have visited our website since then
More visitors every month