Python Program to Find Factorial of a Number Using Recursion

In this python tutorial, you will learn how to Find Factorial of a Number Using Recursion with the if, else and elif statement with the different operators of the python programming language.

How to Find Factorial of a Number Using Recursion?

Let’s take a look at the source code , here the values are given as input by the user in the code, the if, else and elif statements and for loop along with the assignment operators carry out the function.

RUN CODE SNIPPET
# Factorial of a number using recursion

def recur_factorial(n):
   if n == 1:
       return n
   else:
       return n*recur_factorial(n-1)

num = int(input("Enter the integer: "))

if num < 0:
   print("\nSorry, factorial does not exist for negative numbers")
elif num == 0:
   print("\nThe factorial of 0 is 1")
else:
   print("\nThe factorial of", num, "is", recur_factorial(num))

INPUT:

6

OUTPUT:

Enter the integer: 
The factorial of 6 is 720
  1. At the start, we use def recur_factorial(n): where the def keyword is used to define a function and recur_factorial is used to call the function to get the value of the variable n.
  2. We declare an if statement with the condition n == 1: where if it is satisfied, using the return function we return the value of n, if the condition is not satisfied, it moves to the next step which is the else statement where the value obtained after the execution of the equation n*recur_factorial(n-1) is returned using the return function.
  3. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable num with the statements/strings ("Enter the integer: "), we use the int function and declare the  input value as an integer value.
  4. In the STDIN section of the code editor the input values are entered.
  5. We declare another if statement with the condition num < 0 where if it is satisfied, the print function will display the statement, ("\nSorry, factorial does not exist for negative numbers"), if the condition is not satisfied, it moves to the next step which is the elif statement
  6. We declare the elif statement with the condition num == 0, where if the condition is satisfied, the print function will display the statement, ("\nThe factorial of 0 is 1")
  7. If the above elif statement is not satisfied it moves to the next step which is the else statement, where the print function will display the statement, ("\nThe factorial of", num, "is", recur_factorial(num), where the function recur_factorial(num)  holds the final value of the recursive function will be displayed.

NOTE:

  • Recursive functions are functions that calls itself. It is always made up of 2 portions, the base case and the recursive case. The base case is the condition to stop the recursion. The recursive case is the part where the function calls on itself.
  • The < lesser than operator is a comparison operator which returns True if the condition is satisfied, if not it returns false.
  • The == equality is a comparison operator which returns True is the two items are equal and returns False if not equal.
  • The input() function allows a user to insert a value into a program, it returns a string value.
  • The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
  • The elif is short for else if, it allows us to check for multiple expressions.
  •  The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The print statement/string to be displayed in enclosed in double quotes.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...