We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5648ed8 commit 35848b8Copy full SHA for 35848b8
1 file changed
recursion/factorial.py
@@ -1,11 +1,12 @@
1
-def factorial(num):
2
- if num == 1:
3
- return 1
4
- else:
5
- return num * factorial(num - 1)
+num = int(input("Enter a number: "))
6
7
-def main():
8
- print(factorial(7))
+factorial = 1
9
10
-if __name__ == '__main__':
11
- main()
+if num < 0:
+ print("Sorry, factorial does not exist for negative numbers")
+elif num == 0:
+ print("The factorial of 0 is 1")
+else:
+ for i in range(1,num + 1):
+ factorial = factorial*i
12
+ print("The factorial of",num,"is",factorial)
0 commit comments