-
Notifications
You must be signed in to change notification settings - Fork 19
/
factorial.c
40 lines (35 loc) · 898 Bytes
/
factorial.c
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
/*
* This file contains two different versions of the factorial function (n!).
* One of them computes the factorial iteratively (using a loop), and the
* other computes the factorial recursively.
*/
#include <stdio.h>
/*
* This function computes and returns the factorial of n. It does so
* iteratively, i.e. using a loop.
*/
int iterative_factorial(int n) {
int f = 1;
for (int i = 1; i <= n; i++) {
f *= i;
}
return f;
}
/*
* This function computes and returns the factorial of n. It does so
* recursively
*/
int recursive_factorial(int n) {
if (n <= 1) {
return 1;
}
return n * recursive_factorial(n - 1);
}
int main() {
for (int i = 2; i <= 10; i+= 2) {
printf("%d! = ", i);
printf("\t%d (iterative)", iterative_factorial(i));
printf("\t\t%d (recursive)\n", recursive_factorial(i));
}
return 0;
}