Skip to content

Commit 1998d4b

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#191 from rohnux/master
factorial of a given number
2 parents 813b8d9 + cadf64c commit 1998d4b

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

factorial.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
12
def factorial(n):
2-
3-
# single line to find factorial
4-
return 1 if (n==1 or n==0) else n * factorial(n - 1)
5-
6-
num = int(input("Enter a number: "))
7-
print ("Factorial of",num,"is",
8-
factorial(num))
3+
if n < 0:
4+
return 0
5+
elif n == 0 or n == 1:
6+
return 1
7+
else:
8+
fact = 1
9+
while(n > 1):
10+
fact *= n
11+
n -= 1
12+
return fact
13+
14+
num = 5;
15+
print("Factorial of",num,"is",
16+
factorial(num))
17+

0 commit comments

Comments
 (0)