IP-14.c (1422B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 03 Jan 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a program to calculate the factorial of a number 9 (i) using recursion 10 (ii) using iteration 11 */ 12 13 #include <stdio.h> 14 15 long long int fact_tail_rec(int, long long int); 16 long long int fact_rec(int); 17 long long int fact_ite(int); 18 19 int main() 20 { 21 int n; 22 printf("Enter the number: "); 23 scanf("%d", &n); 24 if (n < 0) 25 { 26 printf("\nFactorial of negetive number is not possible."); 27 return 1; 28 } 29 printf("\nFactorial of %d (Tail-Recursion) = %lld", n, fact_tail_rec(n, 1)); 30 printf("\nFactorial of %d (Recursion) = %lld", n, fact_rec(n)); 31 printf("\nFactorial of %d (Iteration) = %lld", n, fact_ite(n)); 32 return 0; 33 } 34 35 long long int fact_tail_rec(int n, long long int result) 36 { 37 if (n == 0 || n == 1) 38 { 39 return result; 40 } 41 else 42 { 43 return fact_tail_rec(n - 1, n * result); 44 } 45 } 46 47 long long int fact_rec(int n) 48 { 49 if (n == 0 || n == 1) 50 { 51 return 1; 52 } 53 else 54 { 55 return n * fact_rec(n - 1); 56 } 57 } 58 59 long long int fact_ite(int n) 60 { 61 int i; 62 long long int result = 1; 63 if (n == 0 || n == 1) 64 { 65 return 1; 66 } 67 for (i = 2; i <= n; i++) 68 { 69 result *= i; 70 } 71 return result; 72 }