# Python program to find factorial of a given number

Python program to find factorial of a given number

Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to calculate the factorial. It is represented by (!).

Example: Factorial of 5 is 5*4*3*2*1 = 120

Source Code

The following is a source code to get the factorial of a given number.

```#Program to find factorial of the given number
#Take input from user
number = int(input("Enter the number to find factorial:"))
count = 1
#Check the number is positive, negative, or zero
if number < 0:
print("Factorial for negative number doesn't exist.")
elif number==0:
print("The factorial will be 1")
else:
while(number>1):
count *=number
number -= 1
print("The factorial of an entered number is", count) ```

Output

CASE 1:

CASE 2:

Explanation:

In this program, a user asks to enter a number whose factorial will be calculated. This number will store in a variable named number. Next, we declare the variable count and initialized its value 1. Now, we will check the number is positive, negative, or zero using if....elif...else statements. If the number is greater than 1, the count's value is increased by multiplying the numbers with the value stored in the count. After each iteration, the value of the number will decrease by 1. When the iteration stopped, the count variable's updated value will be printed, which is the factorial of the number that we have entered to calculate.

Using Recursion

The following is a source code to get factorial number using recursion:

``` #Program to calculate factorial of a number using recursion
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
number = int(input("Enter the number: "))
# check if the number is negative
if number < 0:
print("Factorial for negative number doesn't exist")
elif number == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", number, "is", factorial(number))  ```

Output

CASE 1:

CASE 2:

Explanation:

In this program, the number variable is declared to store the numeric value. This number is then passed to the recursive function factorial() to calculate the factorial. The recursive function calls itself and multiplies the number with the factorial of the number below it until it becomes equal to one. If the number is equal to 1, it will display the value stored in a number variable on the screen.