In this post we will be creating a Python script that will calculate the factorial of any given number. The script will take a number via input and find the factorial before printing to output. To achieve this we will be using the math module, which provides access to the mathematical functions defined by the C standard.
See the snippet of Python code below which uses the ‘factorial()’ function, the function will calculate the factorial of the number given and return this to output.
import math
while True:
Number=int(input("Please Enter Number to find Factorial: "))
print(math.factorial(Number))
An example of the code being run can be seen below.
Please Enter Number to find Factorial: 5
120
Please Enter Number to find Factorial: 6
720
Please Enter Number to find Factorial: 7
5040
Please Enter Number to find Factorial: 8
40320
Please Enter Number to find Factorial: 10
3628800
>>>
Take a look at some of our other content around the Python programming language by clicking here.