pc-ip-13.c (1032B)
1 /* 2 * Question 13: 3 * Write a program to display the Fibonacci series using recursive function and iterative function. 4 */ 5 6 #include <stdio.h> 7 8 long long int fib_rec(int); 9 void fib_rec_print(int); 10 void fib_ite_print(int); 11 12 int main() 13 { 14 int n; 15 printf("Enter the number of terms: "); 16 scanf("%d", &n); 17 fib_rec_print(n); 18 fib_ite_print(n); 19 return 0; 20 } 21 22 void fib_rec_print(int n) 23 { 24 int i; 25 printf("\nFibonacci Series (Recursion):"); 26 for (i = 0; i <= n; i++) 27 { 28 printf(" %lld", fib_rec(i)); 29 } 30 } 31 32 void fib_ite_print(int n) 33 { 34 int i, t1 = 0, t2 = 1, t3; 35 printf("\nFibonacci Series (iteration):"); 36 if (n > 0) 37 { 38 printf(" 0"); 39 } 40 if (n > 1) 41 { 42 printf(" 1"); 43 } 44 for (i = 2; i <= n; i++) 45 { 46 t3 = t1 + t2; 47 printf(" %d", t3); 48 t1 = t2; 49 t2 = t3; 50 } 51 } 52 53 long long int fib_rec(int n) 54 { 55 if (n == 0 || n == 1) 56 { 57 return n; 58 } 59 else 60 { 61 return fib_rec(n - 1) + fib_rec(n - 2); 62 } 63 }