Skip to content

Commit 35848b8

Browse files
committed
factorial in python
1 parent 5648ed8 commit 35848b8

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

recursion/factorial.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
def factorial(num):
2-
if num == 1:
3-
return 1
4-
else:
5-
return num * factorial(num - 1)
1+
num = int(input("Enter a number: "))
62

7-
def main():
8-
print(factorial(7))
3+
factorial = 1
94

10-
if __name__ == '__main__':
11-
main()
5+
if num < 0:
6+
print("Sorry, factorial does not exist for negative numbers")
7+
elif num == 0:
8+
print("The factorial of 0 is 1")
9+
else:
10+
for i in range(1,num + 1):
11+
factorial = factorial*i
12+
print("The factorial of",num,"is",factorial)

0 commit comments

Comments
 (0)