pc-ip-14.c
Problem Statement
pc-ip-14.c
Question 14: Write a program to calculate the factorial of a number using recursive and iterative function.
Source Code
c
#include <stdio.h>
long long int fact_rec(int);
long long int fact_ite(int);
int main()
{
int n;
printf("Enter the number: ");
scanf("%d", &n);
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_ite(int n)
{
int i, pd = 1;
for (i = 1; i <= n; i++)
{
pd *= i;
}
return pd;
}
long long int fact_rec(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * fact_rec(n - 1);
}
}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
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
● Author - Amit Dutta · Updated - 05 Jan 2026