In the above code, we have used the recursion to find the factorial of a given number. We have defined the fact(num) function, which returns one if the entered value is 1 or 0 otherwise until we get the factorial of a given number.,We have imported the math module that has factorial() function. It takes an integer number to calculate the factorial. We don't need to use logic.,Factorial is a non-negative integer. It is the product of all positive integers less than or equal to that number you ask for factorial. It is denoted by an exclamation sign (!).,In the above example, we have declared a num variable that takes an integer as an input from the user. We declared a variable factorial and assigned 1. Then, we checked if the user enters the number less than one, then it returns the factorial does not exist for a negative number. If it returns false, then we check num is equal to zero, it returns false the control transfers to the else statement and prints the factorial of a given number.
Enter a number: 10
The factorial of 10 is 3628800
Factorial of 5 is 120
Enter the number: 6 Factorial of 6 is 720
Note: To test the program for a different number, change the value of num.,Here, the number whose factorial is to be found is stored in num, and we check if the number is negative, zero or positive using if...elif...else statement. If the number is positive, we use for loop and range() function to calculate the factorial.,The factorial of a number is the product of all the integers from 1 to that number.,For example, the factorial of 6 is 1*2*3*4*5*6 = 720. Factorial is not defined for negative numbers, and the factorial of zero is one, 0! = 1.
Factorial of a Number using Loop
# Python program to find the factorial of a number provided by the user. # change the value for a different result num = 7 # To take input from the user #num = int(input("Enter a number: ")) factorial = 1 # check if the number is negative, positive or zero if num < 0: print("Sorry, factorial does not exist for negative numbers") elif num == 0: print("The factorial of 0 is 1") else: for i in range(1, num + 1): factorial = factorial * i print("The factorial of", num, "is", factorial)
Output
The factorial of 7 is 5040
Factorial of a Number using Recursion
# Python program to find the factorial of a number provided by the user # using recursion def factorial(x): "" "This is a recursive function to find the factorial of an integer "" " if x == 1: return 1 else: # recursive call to the function return (x * factorial(x - 1)) # change the value for a different result num = 7 # to take input from the user # num = int(input("Enter a number: ")) # call the factorial function result = factorial(num) print("The factorial of", num, "is", result)
Last Updated : 11 Oct, 2017
Output :
The factorial of 23 is: 25852016738884976640000
This method is defined in “math” module of python. Because it has C type internal implementation, it is fast.
math.factorial(x) Parameters: x: The number whose factorial has to be computed. Return value: Returns the factorial of desired number. Exceptions: Raises Value error if number is negative or non - integral.
Runtime Error :
Traceback(most recent call last):
File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in
print(math.factorial(-5))
ValueError: factorial() not defined
for negative values
Output :
The factorial of -5 is:
Runtime Error :
Traceback(most recent call last):
File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in
print(math.factorial(-5))
ValueError: factorial() not defined
for negative values
Output :
The factorial of 5.6 is:
Runtime Error :
Traceback(most recent call last):
File "/home/3987966b8ca9cbde2904ad47dfdec124.py", line 9, in
print(math.factorial(5.6))
ValueError: factorial() only accepts integral values
Python Code:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
n = int(input("Input a number to compute the factiorial : "))
print(factorial(n))
Sample Output:
Input a number to compute the factiorial: 4
24
Membership Testing in a Collection:
>>> a = ('one', 'two', 'three', 'four', 'five') >>>
if 'one' in a:
...print('The tuple contains one.')
...
The tuple contains one. >>>
b = {
0: 'zero',
1: 'one',
2: 'two',
3: 'three'
} >>>
if 2 in b.keys():
...print('The dict has the key of 2.')
...
The dict has the key of 2.
April 25, 2022April 25, 2022
Let’s see what this means and how we can calculate the factorial for 7!
:
7! = 1 * 2 * 3 * 4 * 5 * 6 * 7 = 5040
Let’s try finding the factorial of 7 using the math
library:
# Finding Factorials with math.factorial() import math print(math.factorial(7)) # Returns: 5040
Similarly, let’s try getting the factorial of a negative number:
# Finding the factorial of a negative number
import math
print(math.factorial(-7))
# Raises: ValueError: factorial() not defined
for negative values
We’ll use the augmented assignment operator to make our code a little slimmer. Let’s see what this looks like:
# Using For Loops to Calculate Factorials
def factorial(number):
num = 1
for i in range(1, number + 1):
num *= i
return num
print(factorial(7))
# Returns: 5040