-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfactorial.c
50 lines (40 loc) · 984 Bytes
/
rfactorial.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
41
42
43
44
45
46
47
48
49
/* vim:ts=4:sw=4:et:
*
* rfactorial.c
* Compute the factorial (n!) of an integer.
* Recursive version.
*
* Description:
* The definition of the factorial function:
* n! = n * (n - 1) * (n - 2) * (n - 3) * ... * 3 * 2 * 1
* Number to factorial comes from the command line or a default
* value if none is passed by command line argument.
*
* Returns:
* Success.
*/
#include <stdio.h>
#include <stdlib.h>
unsigned long rfactorial(unsigned long n);
int main(int argc, char *argv[]) {
unsigned int n = 0U;
if (argc == 2) {
n = atoi(argv[1]);
}
else {
n = 5U;
}
printf("%d! = ", n);
// Show result and exit successfully
printf("%lu\n", rfactorial((unsigned long)n));
return 0;
}
/*
* rfactorial Rcursively compute the facorial of a given non-zero integer.
*/
unsigned long rfactorial(unsigned long n) {
if (n == 1)
return n;
else
return (n * rfactorial(n-1));
}