-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathfactorial.c
42 lines (32 loc) · 808 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
41
42
// C Program to find the Factorial of a number
#include <stdio.h>
long long unsigned int factorial(int num)
{
if (num == 0)
return 1;
return num * factorial(num - 1);
}
int main()
{
int num;
printf("\nEnter the number: ");
scanf("%d", &num);
// Factorials are undefined for negative integers
if (num < 0)
{
printf("Factorials are undefined for negative integers");
}
// Call the function
long long unsigned int fact = factorial(num);
// Print the obtained factorial
printf("The factorial of the given number is: %lld", fact);
return 0;
}
/*
Time Complexity- O(num), where 'num' is the given number
Space Complexity- O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE I
Enter the number: 12
The factorial of the given number is: 479001600
*/