Python-Factorial of a Number


Write a program to compute the factorial of a number entered by the user.

#Factorial of a number
#Factorial of a number n, is the product of all natural numbers from 1 to n
#So, factorial of 5 (denoted as 5!) is 1 x 2 x 3 x 4 x 5 that is 120

def factorial(n):
        f = 1
        for i in range(1, n+1):
                f = f * i
        print ("Factorial = ", f)
#EO Function  - end of function. It helps to put this comment at 
#the end of a function to know where a function has ended. 
#The braces { and } in C, C++, C#, #and Java were useful in that 
#sense to identify a block of code.

#Take input and pass to the factorial function
n = int(input("Enter a no. ")) 

factorial(n)      # call the function

#EndOfProgram
Advertisements
%d bloggers like this: