Factorial using recursion in Python
In this post, you will learn how to find the factorial of a number using recursion in Python programming language.
The factorial is normally used in different areas of mathematics, including algebra, mathematical analysis, and Combinations and Permutations. The factorial of a number n is denoted by n!. This is the product of all positive numbers less than or equal to n. It is calculated as -
n! = n X (n-1) X (n-3) X ……… X 3 X 2 X 1
Example of factorial
Suppose we want to get a factorial of 6, then the factorial will be -
6! = 6 X 5 X 4 X 3 X 2 X 1
720
Python gets factorial using Recursion function
Here is the Python program to find the factorial of a number using the recursion function. When a user enters a number as input, it passes to the function. If the number equals 1, it returns 1. The factorial of a number less than equals to 0 does not exist. If the input is greater than 1, the function calls recursively and finds the factorial of a number.
def factorial(num):
if num == 1:
return num
elif num <= 0:
return " Not Exist"
else:
return num * factorial(num - 1)
# Enter the input
num = int(input("Enter a Number: "))
print("Factorial of", num, " : ", factorial(num))
Output of the above code:
Enter a Number: 7
Factorial of 7 : 5040
Enter a Number: 11
Factorial of 11 : 39916800
Enter a Number: 5
Factorial of 5 : 120
Enter a Number: 8
Factorial of 8 : 40320
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary