CSC111 RecursiveFactorial.py

From dftwiki3
Revision as of 10:29, 30 April 2014 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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()