采用递归调用的方式用用c语言表示斐波那契数列
时间: 2024-03-09 15:32:45 浏览: 35
用C 语言实现斐波那契数列
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
int main() {
int n, i;
printf("Enter the number of terms in the Fibonacci sequence: ");
scanf("%d", &n);
printf("Fibonacci sequence: ");
for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
//样例输出:
//Enter the number of terms in the Fibonacci sequence: 10
//Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34
阅读全文