bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

assignment-s-17.c (1680B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 21 Dec 2025
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a program to display the Fibonacci series
      9     (i) using recursion
     10     (ii) using iteration
     11 */
     12 
     13 #include <stdio.h>
     14 
     15 long long int fib_rec(int);
     16 long long int fib_tail_rec(int, long long int, long long int);
     17 void fib_rec_print(int);
     18 void fib_ite_print(int);
     19 
     20 int main()
     21 {
     22     int n;
     23     printf("Enter the number of terms: ");
     24     scanf("%d", &n);
     25     fib_rec_print(n);
     26     fib_ite_print(n);
     27     return 0;
     28 }
     29 
     30 long long int fib_rec(int n)
     31 {
     32     if (n == 0 || n == 1)
     33     {
     34         return n;
     35     }
     36     else
     37     {
     38         return fib_rec(n - 1) + fib_rec(n - 2);
     39     }
     40 }
     41 
     42 long long int fib_tail_rec(int n, long long int t1, long long int t2)
     43 {
     44     if (n == 0)
     45     {
     46         return t1;
     47     }
     48     else if (n == 1)
     49     {
     50         return t2;
     51     }
     52     else
     53     {
     54         return fib_tail_rec(n - 1, t2, t1 + t2);
     55     }
     56 }
     57 
     58 void fib_rec_print(int n)
     59 {
     60     int i;
     61     printf("\nFibonacci Series (Recursion):");
     62     for (i = 0; i < n; i++)
     63     {
     64         printf("  %lld", fib_rec(i));
     65     }
     66     printf("\nFibonacci Series (Tail-Recursion):");
     67     for (i = 0; i < n; i++)
     68     {
     69         printf("  %lld", fib_tail_rec(i, 0, 1));
     70     }
     71 }
     72 
     73 void fib_ite_print(int n)
     74 {
     75     int i;
     76     long long int t1 = 0, t2 = 1, temp;
     77     printf("\nFibonacci Series (Iteration):");
     78     if (n > 0)
     79     {
     80         printf("  0");
     81     }
     82     if (n > 1)
     83     {
     84         printf("  1");
     85     }
     86     for (i = 2; i < n; i++)
     87     {
     88         printf("  %lld", t1 + t2);
     89         temp = t1;
     90         t1 = t2;
     91         t2 = temp + t2;
     92     }
     93 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror