Skip to content

Commit bd786c5

Browse files
Merge pull request #19 from seancyw/issue_9
Added program to find factorial of number #9
2 parents ba41875 + 9a3ac3c commit bd786c5

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Code/factorial.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
def get_factorial(number):
2-
answer = 1 # We start with 1 as usual for finding factorial
3-
4-
for i in range(1, (number+1)): # 1 is added to second argument of range function since it in this syntax loop terminates at the second value without executing for that value.
5-
answer = answer * i
6-
7-
return answer # answer now contains the calculated value
8-
9-
10-
11-
if __name__ == '__main__':
12-
number = input("Enter the number : ")
13-
number = int(number)
14-
factorial = get_factorial(number)
15-
print ("The factorial of " + number+ " is = " + factorial)
16-
1+
2+
#program to find the factorial of a number provided by a user
3+
4+
if __name__ == "__main__":
5+
number = int(raw_input("Enter a number to get its factorial: "))
6+
factorial = 1
7+
8+
if number < 0:
9+
print("Sorry, factorial doesn't exist for negative numbers!")
10+
elif number == 0:
11+
print("The factorial of 0 is 1")
12+
else:
13+
for i in range(1, number + 1):
14+
factorial = factorial * i
15+
print "The factorial of %r is %r" % (number, factorial)
16+

0 commit comments

Comments
 (0)