-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.py
More file actions
51 lines (37 loc) · 981 Bytes
/
Factorial.py
File metadata and controls
51 lines (37 loc) · 981 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
46
47
48
49
50
51
# Python code to demonstrate math.factorial()
import math
print("The factorial of 23 is : ", end="")
print(math.factorial(23))
#########################################
# Python code to demonstrate naive method
# to compute factorial
n = 23
fact = 1
for i in range(1, n + 1):
fact = fact * i
print("The factorial of 23 is : ", end="")
print(fact)
#########################################
def factorial(n):
# single line code
return 1 if (n == 1 or n == 0) else n * factorial(n - 1)
#########################################
# Multiple line code
if n == 0:
return 1
else:
return n * factorial(n - 1)
*****************************************
# Iterative code
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while(n > 1):
fact *= n
n -= 1
return fact
n = int(input('Check the factorial of:'))
print("Factorial of", n, "is", factorial(n))