**What is factorial**

Factorial of an integer(or a natural number) is the product of all numbers starting from the integer till 1. Example, factorial of 5 will be 5 * 4 * 3 * 2 * 1 = 120.

Thus, factorial of a number n will be n * (n-1) * (n-2) ….. * 1.

**Finding factorial of a number in python**

There are a couple of approaches to find factorial of a number using python. This post will discuss both these approaches.

**Method 1: Iterative approach**

This method finds the factorial of a number by iterating from the number till 1 and finding the product in every iteration. Algorithm for this method comprises of the following steps.

1. Initialize a variable to hold the factorial of number to 1.

2. Loop from the number till 1 with an interval of 1.

3. In every iteration, find the product of number and the variable initialized in step 1 and store it in the same variable

4. After loop completes(when the last number is 1) the variable will be holding the product of all numbers till 1 which is the factorial of the given number.

Python program written as per the above algorithm is given below.

# function to calculate factorial of the given number def fact(number): # initialize variable to hold factorial fact = 1 # loop from the number till 1 for number in range(5, 1,-1): # multiply current number with the product of previous numbers fact = fact * number return fact # read number from keyboard number = int(input("Enter a number\n")) # find factorial factorial = fact(number) print("Factorial is "+str(factorial)) |

Above program contains a function to calculate factorial of a number. It initializes a variable to hold the factorial and reverse iterates from the number till 1 using a `for`

loop and `range`

function. `range`

function takes 3 arguments, first is the start value which will be the number itself, second argument is the end number till which you want the loop to execute. This value will be 1 and third argument is the step value with which we want to iterate. Since we want to reverse iterate by 1 every time, this value will be -1.

In every iteration, we multiply the current value with the value stored in the variable. Thus, in every iteration, this variable will hold the product of a number with all numbers before it and at the end of the loop it will have the product of all numbers till 1.

Number whose factorial needs to be calculated is read from the keyboard using `input`

function. Since this function returns the value in string format, we need to convert it to an integer using `int`

function.

Output of the above program is as shown below.

Enter a number

5

Factorial is 120

It is also possible to use `while`

loop in place of `for`

loop in this method with a slight modification. Loop will continue till the number is greater than equal to 1 and in each iteration, the number is decreased by 1 inside the loop as shown below.

def fact_while(number): # initialize variable to hold factorial fact = 1 # loop till the number reaches 1 while number >= 1: # multiply current number with the product of previous numbers fact = fact * number # reduce the number by 1 number = number - 1 return fact |

Output remains the same.

**Method 2: Recursive approach**

Recursion means a method calling itself till some condition is met. A method which calls itself is called a recursive method. A recursive method should have a condition which must cause it to return else it will keep on calling itself infinitely resulting in memory over flow.

Calculating the factorial of a number using recursive method should work on the following algorithm.

1. Create a method which accepts one argument.

2. Check if the value of argument is 1, then return 1 from this method. This will be the terminating condition of the recursive method.

3. Return the product of the argument and the return value of this method called with (argument – 1).

Code follows

# recursive method which returns the factorial of a number def factorial_recursion(number): # check if number is 1 if number == 1: # return 1 from the method return 1 return number * factorial_recursion(number-1) # factorial = factorial_recursion(5) # read number from keyboard number = int(input("Enter a number\n")) # find factorial factorial = fact(number) print("Factorial is "+str(factorial)) |

The method will keep on calling itself and return the product of argument supplied to it with 1 less than the argument. When the argument value is 1, it will return 1. This way, the recursive method will return the product of all the numbers starting from the argument till 1.

Output of the above program will be

Enter a number

5

Factorial is 120

Hope this post helped you to understand the logic of finding the factorial of a number in python using iterative and recursive approaches. Keep visiting!!!