Factorial of a number:
The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, denoted by n! in mathematics:
n! = n * (n – 1) *(n – 2) * . . . . . . . . . . 3 * 2 * 1.
4 != 4 * 3 * 2 * 1
Examples:
Input:
number = 7
Output:
Factorial of 7 = 5040
Given a number, the task is to find the factorial of the given number
- Python Program for Double Factorial
- Python Program to Count Divisors of Factorial
- Python Program to Compute 1/N!
Finding Factorial of a Number
There are several ways to find factorial of a number in python some of them are:
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
Method #1:Using for loop(Without Recursion)
Approach:
- Create a variable say res and assign it to 1.
- Using for loop and range, Run a loop from 1 to n.
- Multiply the res with the loop iterator value.
Below is the implementation:
# given number num = 7 # initializing a variable res with 1 res = 1 # Traverse from 1 to n for i in range(1, num+1): res = res*i # print the factorial print("Factorial of", num, "=", res)
Output:
Factorial of 7 = 5040
Method #2: Using recursion
To find the factorial of a given number, we will use recursion. We defined the factorial(num) function, which returns 1 if the entered value is 1 and 0 otherwise, until we get the factorial of a given number.
Below is the implementation:
# function which returns the factorial of given number def facto(num): if(num == 1 or num == 0): return 1 else: return num*facto(num-1) # given number num = 7 # passing the given num to facto function which returns the factorial of the number res = facto(num) # print the factorial print("Factorial of", num, "=", res)
Output:
Factorial of 7 = 5040
Method #3 : Using Built in Python functions
Python has a built-in function
factorial (number)
which returns the factorial of given number.
Note: factorial function is available in math module
Below is the implementation:
# import math module import math # given number num = 7 # finding factorial using Built in python function res = math.factorial(num) # print the factorial print("Factorial of", num, "=", res)
Output:
Factorial of 7 = 5040
Related Programs:
- python program to find the factors of a number
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to find the power of a number using recursion
- python program to compute the power of a number
- python program to count the number of digits present in a number
- python program to find whether a number is a power of two
- program to clear the rightmost set bit of a number in cpp and python
Related Programs:
- python program to find the factors of a number
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to find the power of a number using recursion
- python program to compute the power of a number
- python program to count the number of digits present in a number
- python program to find whether a number is a power of two
- program to clear the rightmost set bit of a number in cpp and python