1、需求:键盘输入序列号,输出第序列号个菲波拉契数
/*
菲波拉契序列
1 2 3 5 8 13 21 34
*/
2、代码:
#include <stdio.h>
int main()
{
int f1 = 1;
int f2 = 2;
int f3;
int n;
printf("请输入序列数:");
scanf("%d",&n);
if (n == 1){
f3 = f1;
}else if (n == 2){
f3 = f2;
}else {
for (int i = 3;i <= n;i++){
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
}
printf("%d",f3);
return 0;
}
3、输出结果:
请输入序列数:4
5
...Program finished with exit code 0
Press ENTER to exit console.