1） non-recursive
Define first f1,f2 Give the values of the first two terms of Fibonacci series , Redefinition f3 Save the sum of the first two items , Which is the value of the next item , Constantly changing assignments in recycling , Finally, find out the n term .
#include<stdio.h> #include<stdlib.h> int fabonacci(int n) { int f1 = 1; int f2
= 1; int i = 0, f3 = 1; for (i = 3; i <= n; i++) { f3 = f1 + f2; f1 = f2; f2 =
f3; } return f3; } int main() { int a = 0; printf(" Please enter a Value of :"); scanf("%d", &a);
printf("%d\n", fabonacci(a)); system("pause"); return 0; }
2） recursion
Find out that the termination condition of recursion is that the values of the first and second terms are equal to 1, Then according to the formula f(n)=f(n-1)+f(n-2) We can get the conclusion that n term .
#include<stdio.h> #include<stdlib.h> int facibo(int n) { if ((n == 1) || (n ==
2)) { return 1; } else { return facibo(n - 1) + facibo(n - 2); } return 0; } int
main() { int a = 0; printf(" Please enter a Value of :"); scanf("%d", &a); printf("%d\n", facibo(a)
); system("pause"); return 0; }

Technology
Daily Recommendation