Python @ DjangoSpin

PyPro #30 Computing Factorial of a Number

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Computing Factorial of a Number in Python: Compose a factorial() function which accepts an integer and returns its factorial. Factorial (denoted by an exclamation mark !) of a number x is the product of an x and all the integers below it till 1. Example: 3! = 3 * 2 * 1 i.e. 6. Also, by mathematical definition, 0! = 1. In order to build your analytical skills, do NOT use the factorial() function of builtin module math.

Computing Factorial of a Number in Python

def computeFactorial(num):
    fact = 1
    while True:
        if num == 1:
            return fact
        else:
            fact = fact * num
            num = num - 1
            

enteredNumber = int(input("Please enter a number to calculate its factorial: "))
factorial = computeFactorial(enteredNumber)

print(factorial)
Try it here.

See also:


Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon