用C++语言写一段程序,输出第n个Fibonacci数。说明: Fibonacci数依次为:1,1,2,3,5,8,……,其规律为: Fi=1 (n=1) F2=1 (n=2) Fn=Fn-1+Fn-2 (n>=3)
时间: 2024-05-07 10:16:39 浏览: 50
电大《C++语言程序设计》第1、2、3、4次作业及答案.doc
Sure, here is a simple C program to output the nth Fibonacci number:
```
#include <stdio.h>
int fibonacci(int n);
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("The %dth Fibonacci number is %d\n", n, fibonacci(n));
return 0;
}
int fibonacci(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
```
Please note that this program assumes that the user enters a positive integer value for n.
阅读全文