summary refs log tree commit diff stats
path: root/assignments/27-fac-rec.c
blob: 13f00d3c670f7e20100fcbe8670d624a3ab79c81 (plain) (blame)
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
#include <stdio.h>

int fact(int n)
{
	if (n == 0)
		return 1;
	else
		return n * fact(n - 1);
}

int main(void)
{
	int n;
	printf("To calculate the factorial of a given number\n\n");
	printf("Enter a number: ");
	scanf("%d", &n);
	if (n < 0) {
		printf("Invalid input, must be greater than -1\n");
		return 0;
	}
	printf("%d! = %d\n", n, fact(n));
	return 0;
}

/*
Output:
Set 1:
To calculate the factorial of a given number

Enter a number: -1
Invalid input, must be greater than zero

Set 2:
To calculate the factorial of a given number

Enter a number: 0
0! = 1

Set 3:
To calculate the factorial of a given number

Enter a number: 5
5! = 120

Set 4:
To calculate the factorial of a given number

Enter a number: 10
10! = 3628800

*/