CSC111 RecursiveFactorial.py

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 13:03, 26 April 2010 (UTC)


# factorial.py
# Demonstrates a recursive factorial function

# fact( n ): computes the factorial of a number n
# returns an int, which is the factorial of n
def fact( n ):

    # stopping condition
    if n<=1: 
        return 1

    # recursive step
    return n * fact( n-1 )


def main():
    # print first 15 factorials 
    for n in range( 1, 15 ):
        print "%4d! = %20d" % ( n, fact( n ) )

main()