IP-14.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 03 Jan 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Write a program to calculate the factorial of a number (i) using recursion (ii) using iteration
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
long long int fact_tail_rec(int, long long int);
long long int fact_rec(int);
long long int fact_ite(int);
int main()
{
int n;
printf("Enter the number: ");
scanf("%d", &n);
if (n < 0)
{
printf("\nFactorial of negetive number is not possible.");
return 1;
}
printf("\nFactorial of %d (Tail-Recursion) = %lld", n, fact_tail_rec(n, 1));
printf("\nFactorial of %d (Recursion) = %lld", n, fact_rec(n));
printf("\nFactorial of %d (Iteration) = %lld", n, fact_ite(n));
return 0;
}
long long int fact_tail_rec(int n, long long int result)
{
if (n == 0 || n == 1)
{
return result;
}
else
{
return fact_tail_rec(n - 1, n * result);
}
}
long long int fact_rec(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * fact_rec(n - 1);
}
}
long long int fact_ite(int n)
{
int i;
long long int result = 1;
if (n == 0 || n == 1)
{
return 1;
}
for (i = 2; i <= n; i++)
{
result *= i;
}
return result;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60