Guest User

Untitled

a guest
Jun 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. def factorial(x):                            # ex. 10
  2.     if (x == 0):
  3.         print '1';
  4.     if (x < 0):
  5.         print 'Negative integers not allowed'
  6.        
  7.     temp = x                                 # temp = 10
  8.     x = x-1                                  # x = 10-1 = 9
  9.     subtotal = temp*x                        # 10*9 = 90
  10.     total = subtotal                        
  11.    
  12.     while (x > 2):                                    # then:
  13.         temp = x-1                           #  = 8          #  = 6             #  = 4           #  = 2
  14.         x = x-2                              #  = 7          #  = 5             #  = 3           #  = 1
  15.         subtotal = temp*x                    #  = 8*7        #  = 6*5           #  = 4*3         #  = 2*1
  16.         total = total * subtotal             #  = 10*9*8*7   #  = 10*9*8*7*6*5  #  = 10*...*4*3  #  = 10*...*2*1
  17.        
  18.     print "The factorial of ",x," is ",total
  19.        
  20. number = input ("Enter a number: ")
  21. factorial(number)
Add Comment
Please, Sign In to add comment