Python Program Factorial Three Versions – This Python tutorial explains three different source codes of the Python factorial program.
Write a program that asks the user for a number and prints out the factorial of that number:
- Using factorial() function of math module
- Using for loop
- Using while loop
The Python has a math module. This math module contains a factorial() function that takes a positive integer as argument and returns the factorial of that number.
How to use Python factorial() function
We will use factorial() function after importing math module in our Python program.
Python Program Factorial Three Versions
The Source code of the Python factorial Program using factorial function
The output is: Enter a number to find its factorial:4 The Factorial of 4 is: 24
Python Factorial Program using for loop
The output is as follows: Enter a number to find its factorial:5 The Factorial of 5 is: 120
Python Factorial program using while loop
The output of factrial program using while loop is: Enter a number to find its factorial:6 The Factorial of 6 is: 720