bsc

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

root / semester_1 / practice-c / pc-ip-14.c

pc-ip-14.c (695B)


      1 /*
      2  * Question 14:
      3  * Write a program to calculate the factorial of a number using recursive and iterative function.
      4  */
      5 
      6 #include <stdio.h>
      7 
      8 long long int fact_rec(int);
      9 long long int fact_ite(int);
     10 
     11 int main()
     12 {
     13     int n;
     14     printf("Enter the number: ");
     15     scanf("%d", &n);
     16     printf("\nFactorial of %d (Recursion): %lld", n, fact_rec(n));
     17     printf("\nFactorial of %d (Iteration): %lld", n, fact_ite(n));
     18     return 0;
     19 }
     20 
     21 long long int fact_ite(int n)
     22 {
     23     int i, pd = 1;
     24     for (i = 1; i <= n; i++)
     25     {
     26         pd *= i;
     27     }
     28     return pd;
     29 }
     30 
     31 long long int fact_rec(int n)
     32 {
     33     if (n == 0)
     34     {
     35         return 1;
     36     }
     37     else
     38     {
     39         return n * fact_rec(n - 1);
     40     }
     41 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror