Python program to find factorial of a number
Contents
The Factorial of a number is the product of all the integers from 1 to that number.For example The factorial of 5 is 120 that is calculated as (1*2*3*4*5).
Method 1: factorial( ) function
Python providing the standard math library to find a factorial of a number in a single step.We can calculate the factorial of the number using factorial function in python as below.
Program
1 2 3 4 5 |
import math print ("The factorial of 5 is: ",math.factorial(5)); print ("The factorial of 6 is: ", math.factorial(6)); print ("The factorial of 7 is: ", math.factorial(7)); print ("The factorial of 8 is :", math.factorial(8)); |
Output:
The factorial of 5 is: 120
The factorial of 6 is: 720
The factorial of 7 is: 5040
The factorial of 8 is : 40320
Method 2: Using the for loop to find the factorial of a number
Program
1 2 3 4 5 6 7 |
number=int(input("Enter a number:")) fact=1 for i in range(1,number+1): fact=fact*i print("The factorial of ",number,"is:",fact); |
Output: