# How to Write a Factorial Program in Python

This is a blog which explains the factorial program.

The factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720.

Factorial is not defined for negative numbers and the factorial of zero is one, 0! = 1.

In this tutorial, we will write a Python program to calculate the factorial of a number provided by the user.

Recursion in Python

A function that calls itself is known as a recursive function. And this process is known as recursion.

A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.

There are two laws of recursion:

1) A recursive algorithm must have a base case.

2) A recursive algorithm must change its state and move toward the base case.

In math, the factorial of a number is the product of all positive integers less than or equal to that number. It’s denoted by an exclamation point and is commonly used in combinations and permutations (two other mathematical concepts you may have heard of).

It’s easy to write a factorial program in Python using recursion or loops. Here’s an example using recursion:

Python is a high level programming language that is widely used for general purpose programming. It was created by Guido van Rossum, and released in 1991. Python is popular because it’s easy to learn, and it’s easier to read than other languages. To write a factorial program in Python, we take the number from the user and then use the mathematical formula to calculate the factorial of a number.

The definition of factorial is n! = n * (n-1)!, where n > 1, and 1! = 1. The factorial of 0 (zero) is defined as being 1. So, if the value of n is either 0 or 1, then the factorial returned is 1. In Python, we can solve the factorial problem using both recursive and iterative approach.

Let us go through both these approaches one by one:

There are two ways to write the factorial program in python.

Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720.

1.Using for loop:

def factorial(num):

product=1

for i in range(1,num+1):

product=product*i

return product

num=15

print(“Factorial of”,num,”is”,factorial(num))

Output: Factorial of 15 is 1307674368000

In the above program, we have defined a function named factorial(). The function takes one argument which is num, value of num is passed by the user and it returns the factorial of that number. The for loop takes care of that. For example, if num=5, then function returns 120 (5!=120). The final result is printed using the print() function.

We will use a recursive function to find the factorial of a number. A recursive function is one that calls itself. It’s easier to write than to explain, so let’s look at an example.

Let’s take a look at the factorial program in Python

Python Program for Find Factorial of Number Using Recursion

Factorial of a number is calculated by multiplying it with all the numbers below it starting from 1. For example factorial of 6 is 6*5*4*3*2*1 which is 720. Factorial of 0 is 1.

Below I have written a python program to find the factorial of a number provided by the user. It uses a for loop to iterate through the values from 1 to ‘num’ and calculate the factorial.

Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one.

For example: Factorial of 4 is 4! = 4 x 3 x 2 x 1 = 24

The program to find the factorial of a number can be implemented in many ways, but here in this article, I will show you program using recursion. A recursive function is a function that calls itself until some condition is met. In the case of factorial, it will call itself until the number becomes 1.

As we are using a recursive function to find the factorial, so there are two cases that we have to consider in our program:

1. If the number is less than 0, an invalid value is returned.

2. The factorial of 0 or 1 is 1.