In this post, we will discuss how to find factorial in python. We will see different ways to find the factorial of a number. In mathematics, the factorial of a positive integer number specifies a product of all integers from 1 to that number. It is defined by the symbol explanation mark (!).

**General case** for finding factorial

*factorial(n) = n * factorial(n-1)*

Factorial (n) =*1 * 2 * … * (n-1) * n*

Or,*n * (n-1) * … * 2 * 1*

The **base case** for finding factorial,

*factorial(0) = 1Or,factorial(1) = 1*

For example: The factorial of 4 is denoted as 4! = 1*2*3*4 or 4*3*2*1 = 24.

We can write it as,

4! = 4 * 3!

Similarly, we can write

3! = 3 * 2!

2! = 2 * 1!

1! = 1 * 0!

0! = 1

Note:- The factorial of a negative number and float number doesn’t exist. The value of 0! is 1, according to the convention for an empty product.

## Factorial Program in Python using For Loop

This is the simplest and easiest way to find the factorial of a number program in python. We will take a number while declaring the variables. Python program to find factorial of a number using for-loop and result will be displayed on the screen.

```
# Python program to find the factorial of a number
# take input
num = int(input("Enter number: "))
# check number is positive, negative, or zero
if num < 0:
print('Factorial does not exist for negative numbers')
elif num == 0:
print('The factorial of 0 is 1')
else:
# find factorial of a number
fact = 1
for i in range(1,num + 1):
fact = fact*i
print('The factorial of',num,'is',fact)
```

Output for the different input values:-

Enter number: 5

The factorial of 5 is 120

Enter number: 12

The factorial of 12 is 479001600

Enter number: -8

Factorial does not exist for negative numbers

Enter number: 0

The factorial of 0 is 1

In this program, inputs are scanned using the input() function and stored in the variable num.

`num = int(input("Enter number: "))`

Check number is positive, negative, or zero. Factorial is not defined for negative numbers and the factorial of zero is one, 0! = 1. Then, find the factorial of a number using for-loop and the result will be displayed on the screen.

```
if num < 0:
print('Factorial does not exist for negative numbers')
elif num == 0:
print('The factorial of 0 is 1')
else:
# find factorial of a number
fact = 1
for i in range(1,num + 1):
fact = fact*i
print('The factorial of',num,'is',fact)
```

## Python Program using While Loop

In the previous program, find the factorial of a number using for loop but in this program, find the factorial of a number using while loop.

```
# Python program to find the factorial of a number
# take input
num = int(input("Enter number: "))
# check number is positive, negative, or zero
if num < 0:
print('Factorial does not exist for negative numbers')
elif num == 0:
print('The factorial of 0 is 1')
else:
# find factorial of a number
fact = 1
i = 1
while(i <= num):
fact = fact*i
i = i+1
print('The factorial of',num,'is',fact)
```

Output:-

Enter number: 4

The factorial of 4 is 24

In this program, we will find factorial of a number using while loop.

```
fact = 1
i = 1
while(i <= num):
fact = fact*i
i = i+1
print('The factorial of',num,'is',fact)
```

In the above program, find the factorial of a number in python using for loop and while loop but many different ways to find factorial of a number in python.

Similar Program to find factorial in Python:-

- Python program to find factorial using math.factorial()
- Python program to find factorial using recursion
- Program to find factorial using the function

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!