Factorial Using Recursion in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

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

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 33 is 3!=3213! = 3 * 2 * 1 which equals 66.

Factorial of 0 is 1, and there is no factorial output for negative integers

working of factorial recursion

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 00, the factorial function returns 11 (since the factorial of 00 is 11) 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.

Read More: