In this simple python program, we need to find the factorial of a number using recursion. It's a python program on recursive function.
To understand this example, you should have knowledge of the following Python programming topics:
In this python program, we have to find the factorial of a given number using a recursive function in python. So we have to understand what you mean by factorial. The Factorial of a number is the product of all the numbers from 1 to that number.
For example, the factorial of a number 5 is 5 * 4 * 3 * 2 * 1 is 120. Now we have to understand what recursion is? Recursion is a function that calls itself repeatedly directly or indirectly. Here we have to call the factorial function recursively with a parameter number less than one every time. And then multiply the number with that recursive function with a parameter with less than one.
return num * factorial(num - 1)
In this python program, we accept the number from the user and save that number in a variable using int
datatype. Now check the number is less than zero. If so, print, we cannot do factorial of that number. Now check the number is zero; if so, then print the factorial of that number is 1. Else we call the function factorial(num) that calculates the factorial using recursion. Inside the function, we check the number is 1 or not using the if
condition in python. If so, return the number. Else
call the function return num * factorial(num - 1).
STEP 1: Enter the number from the user using the input function in python. And convert that string to an integer and store it in a variable.
STEP 2: Use the if
condition to check the number is greater than zero. If the number is less than zero, then print factorial for negative numbers cant be found.
STEP 3: Use the elif
condition to check the number is equal to zero. Then print factorial of zero is 1.
STEP 4: Call the function factorial and pass the number as a parameter. and print the result as the factorial of the number Defining the user function Factorial in python programming
STEP 1: Use the def function to define the function factorial with a parameter.
STEP 2: Check if the number is equal to 1 using an if
condition. Then return num.
STEP 3: Return the result as a number multiplied with a function factorial of a number less than 1.
def factorial(num): # recursive function that call itself to find the factorial of the number
if num == 1:
return num
else:
return num * factorial(num - 1) # finding factorial by multiplying number with factorial of that number less than one,
num = int(input("Enter a Number to find the factorial: ")) # accept a number in python using input function
if num < 0:
print("Factorial for negative numbers cant be calculated") # check the number is negative or not and print cannot find factorial for negative numbers
elif num == 0:
print("Factorial of 0 is 1") # if the number is 0 then print factorial of number 0 is 1
else:
print("Factorial of the number is: ", factorial(num)) # calling the factorial function using number as parameter
Enter a Number to find the factorial: 5 Factorial of the number is: 120