forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.c
More file actions
45 lines (33 loc) · 691 Bytes
/
Copy pathfactorial.c
File metadata and controls
45 lines (33 loc) · 691 Bytes
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
/*
This program outputs the factorial of a number e.g. 5! = 5 * 4 * 3 * 2 * 1 = 120.
In general n! = n * n-1 * n-2* ... * 3 * 2 * 1
By: randerson112358
*/
# include<stdio.h>
int fact_recursive(int n);// This is a recursive factorial function
int fact_iterative(int n);// This is a iterative factorial function
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("%d! = %d\n", n, fact_iterative(n));
system("pause");
}
fact_recursive(int n)
{
//Base Case
if(n == 0)
return 1;
return n *fact_recursive(n-1);
}
int fact_iterative(int n)
{
int i;
int product = 1;
for(i = 1; i<= n; i++)
{
product = product * i;
}
return product;
}