In this tutorial, we are going to learn writing python program to find the factorial of the number using recursion method. We will take a number from the users as and input and our program will print the factorial as an output.
Problem Statement
For any numbers that are input by the user, we have to calculate the factorial of that numbers.
For example
Case1: If the user input the 5.
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā then the output should be 120 ( 1*2*3*4*5= 120).
Case2: If the user input the 6.
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā then the output should be 720 (1*2*3*4*5*6 =720).
What is Factorial of a number?
The factorial of a number is the multiplication of each and every number till the given number except 0.
For example:
Case 1: factorial of number 4 is the multiplication of every number till number 4, i.e.Ā 1*2*3*4
Case 2: factorial of number 3 is the multiplication of every number till number 3, i.e.Ā 1*2*3
NOTE:
- The factorial of 0 is 1
- The factorial of any negative number is not defined.
- The factorial only exits for whole numbers.
What is recursion?
Recursion is a method that defines something in terms of itself, which means it is a process in which a function calls itself. A complete function can be split down into various sub-parts in recursion. It makes code look simple and compact. It is also defined as the process of defining a program or problem in terms of itself.
Letās have a look at the simple diagram that explains the working of recursion.

Our logic to find factorial of a number using recursion
- Our program will take an integer input from the user which should be a whole number.
- If the number input is negative then print āThe factorial canāt be calculatedā.
- Then use fact(num) user-defined recursive function that will return the factorial of the given number i.e. num.
Letās understand through a diagram how our recursive function will work on input 7.

Python Program to find factorial of a number using recursion
def fact(n):
if n == 1:
return n
else:
#recursion
return n*fact(n-1)
num = int(input("Enter a whole number to find Factorial: "))
if num < 0:
print("Factorial can't be calculated for negative number")
elif num == 0:
print("Factorial of 0 is 1")
else:
print("Factorial of",num,"is",fact(num))
Output 1:
Enter a whole number to find Factorial: 5
Factorial of 5 is 120
Explanation:

Output 2:
Enter a whole number to find Factorial: 6
Factorial of 6 is 720
Explanation:

Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…