Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def factorial(x): # ex. 10
- if (x == 0):
- print '1';
- if (x < 0):
- print 'Negative integers not allowed'
- temp = x # temp = 10
- x = x-1 # x = 10-1 = 9
- subtotal = temp*x # 10*9 = 90
- total = subtotal
- while (x > 2): # then:
- temp = x-1 # = 8 # = 6 # = 4 # = 2
- x = x-2 # = 7 # = 5 # = 3 # = 1
- subtotal = temp*x # = 8*7 # = 6*5 # = 4*3 # = 2*1
- total = total * subtotal # = 10*9*8*7 # = 10*9*8*7*6*5 # = 10*...*4*3 # = 10*...*2*1
- print "The factorial of ",x," is ",total
- number = input ("Enter a number: ")
- factorial(number)
Add Comment
Please, Sign In to add comment