Python Program to find the Factorial of a number

Python Program to find the Factorial of a number

In this, we discuss how to find the factorial of a number using Python Programming. So, in this section we cover two examples related to find the factorial. In the first example we discuss how to find factorial using loop and takes input value from the user. In the second example check how to find the factorial using recursion method. 


Now, take a look at what is factorial of a number.


Factorial of a Number - 


Factorial of number is the product of all numbers between 1 and number itself. To get the factorial of a given number, let's make a program by using loop over a range from 1 to itself. 

Remember that range () function does not include a stop value. Therefore the set value should be one more than the input number.

To understand this concept, you need to be familiar with the following Python basic topics:

Python if ... else Statement
Loop Python
Function

Python basic topics are:

Introduction to Python
Basics of Python 


To find out the factorial number takes the input from user and find the factorial of that number.

For example, the factorial of 6 is 1 * 2 * 3 * 4 * 5 * 6 = 720


Example 1: To find the factorial of number and it takes input from user


fact  = 1num = int(input('enter a number'))
m = 1while(m<=num):
    fact = fact * m
    m = m+1print('factorial of entered number is:')
print(fact)
Output:
enter a number 5
factorial of entered number is:
120

In this, example you'll learn how to find factorial of a number using function.


Example 2: To find the factorial of number using function

def factorial(n):
    if(n==0):
        return 1    else:
        return n*factorial(n-1)
n = int(input('enter a number'))
print('factorial of entered number')
fact=factorial(n)
print(fact)
Output:
enter a number 6
factorial of entered number
720