Skip to content

Commit fa3641e

Browse files
authored
Create factorial.py
1 parent f24a01b commit fa3641e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
num = int(input("Enter a number: "))
2+
3+
factorial = 1
4+
5+
# check if the number is negative, positive or zero
6+
if num < 0:
7+
print("Sorry, factorial does not exist for negative numbers")
8+
elif num == 0:
9+
print("The factorial of 0 is 1")
10+
else:
11+
for i in range(1,num + 1):
12+
factorial = factorial*i
13+
print("The factorial of",num,"is",factorial)
14+
15+
"""
16+
Or you can directly use the math.factorial() in python
17+
18+
import math
19+
num = int(input("Enter a number: "))
20+
print ("The factorial is ",math.factorial(num), end="")
21+
"""

0 commit comments

Comments
 (0)