Factorial Using Recursion in Python
Learn via video course

Overview
Recursion is a method in which a function directly or indirectly calls itself. Whereas in Mathematics, the factorial of a number means the product of all positive integers from 1 to that number. An exclamation mark after the number is used to denote that it is a factorial For example 5!.
Scope
- In this article, we will learn how to find the factorial of a number using recursion in python.
- Pre-requisites for this article:
Introduction
Factorial using Recursion can be broken into two parts: Factorial and Recursion.
Let us understand what factorial is using recursion with the help of an example in which a user inputs a number n. The n factorial is n! = n * (n-1)!
Example: factorial of is which equals .
Factorial of 0 is 1, and there is no factorial output for negative integers
A positive integer is passed as an input parameter to the factorial function. So now, whenever the factorial function is executed, the function multiplies the value of the input parameter and recursively calls itself while decrementing the parameter value by 1.
For Example - If factorial(5) is called, it will call factorial(4), factorial(3), factorial(2) and factorial(1). So it means the factorial function will keep calling itself by reducing value by one till it reaches 1.
If the value of the parameter becomes , the factorial function returns (since the factorial of is ) and returns the final multiplied value, which is the factorial of the number passed as an input.
Python Program to Find Factorial of Number Using Recursion
Below is the python program to calculate factorial using recursion for a number.
Output:
In the above program, factorial() is a recursive function that calls itself. Here, the function will recursively call itself by decreasing the value of the n (where n is the input parameter), i.e. n*factorial(n-1). A number is taken as input, and then if-else conditional statements are used to check whether that input number is valid or not, which means that the factorial using recursion function will be executed only if the input value given by the user is greater than or equal to 1.
Python Program to Find Factorial of Number Using Iterative method
Below is the python program to calculate the factorial of a number using For-Loop.
Output
Here, the number whose factorial is to be found is taken as input from the user and stored in num, and then we can check if the number is negative, zero or positive using an if-else statement. If the number is positive, we will use the for-loop and range() function to calculate the factorial. Else, the appropriate statement is printed.
Conclusion
- In a factorial using recursion program, the factorial function calls itself. Here, the function will recursively call itself by decreasing the value of the number.
- The factorial of 0 is 1, and there is no factorial output for negative integers.