forked from utkarsh-shekhar/basic-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.py
More file actions
32 lines (27 loc) · 765 Bytes
/
factorial.py
File metadata and controls
32 lines (27 loc) · 765 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
patch-1
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# change the value for a different result
print(factorial(5))
##### an alternative model for the negative number to put them equal to one. because does not exist Factorial for negative numbers. ####
n=int(input("Enter number:"))
k=1
while(n>0):
k=k*n
n=n-1
print("Factorial of the number is: ")
print(k)
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
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
print("The factorial of",num,"is",factorial)