Python Program to Find the Factorial of a Number
Aim:
Algorithm1:
- step : Start
- step : Declare the variable n and read n value
- n = input(‘ enter the number ‘)
- step : Initialise fact = 1 and i = 1
- step : In while loop perform
- fact = fact * i
- i = i + 1
- step : print factorial of n
- step : end
Example1: Calculate the Factorial of a Number Source Code:
n = int(input('enter the number :'))
fact1 = 1
i = 1
while n >= i:
fact2 = fact1 * i
i = i + 1
print(f"factorial number of n is {fact2}")
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter the number :10
factorial number of n is 1
factorial number of n is 2
factorial number of n is 3
factorial number of n is 4
factorial number of n is 5
factorial number of n is 6
factorial number of n is 7
factorial number of n is 8
factorial number of n is 9
factorial number of n is 10
Process finished with exit code 0
Algorithm2:
- Start
- import math or you can directly use import math.factorial
- print any message
- Then print(math.factorial(number))
Example2: Find Factorial of a Number using math.factorial() Method
import math
print('The factorial of 22 is: ', end='')
print(math.factorial(22))
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
The factorial of 22 is: 1124000727777607680000
Process finished with exit code 0
FAQ:
What is factorial number in Python?
The factorial number is such a number that starts from 1 because factorial of 0! is 1 and the formula to find the factorial number is n! = n*(n-1)*(n-2)*(n-3)*….*3*2*1
How do you do Factorials in Python?
You can use this formula to do factorial of any number i.e. n! = n*(n-1)*(n-2)*(n-3)*….*3*2*1. For example factorial of 5! = 5*4*3*2*1 =120
Does Python have factorial?
Many people don’t know that python has a simple way to print factorial of any number easily. For that, you have to import a package called import math and then you can call the factorial method. Example:
import math
print(‘the factorial of 22 is: ‘, end=”)
print(math.factorial(22))
How do you find the factorial of a number?
You can find the factorial of a number in a different way in python. The first and simple way is by importing a package called math. Example
import math
print(‘the factorial of 22 is: ‘, end=”)
print(math.factorial(22))
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers