[C]recursion递归计算阶乘
计算阶乘
#include <stdio.h> double fact(int); int main() { int x; printf("input a positive integer (<20) to calculate its factorial: "); scanf("%d", &x); printf("Factorial of %d is %.2f\n", x, fact(x)); return 0; } double fact(int x) { if (x == 1) return 1; else return x * fact(x-1); }
generate fibonacci series
#include <stdio.h> double fibon(int); int main() { for (int i = 1; i <= 20; i++) //生成前20个序列1,1,2,3,5,8... { printf("%f\n", fibon(i)); } return 0; } double fibon(int x) { if (x == 1) return 1; if (x == 2) return 1; if (x > 2) return fibon(x-1) + fibon(x-2); }