How to Find Factorial of a Number in Python
In mathematics, the factor of n, a positive integer denoted by n, is the product of all positive integers equal to or less than n:
n! = n * (n-1) * (n-2) * (n-3)* 3 * 2 * 1 .
for example,
5! = 5 * 4 * 3 * 2 1 = 120
A value of 0! 1 is by convention for an empty product.
Factual operations are encountered in many areas of mathematics, particularly combinatorics, algebra, and mathematical analysis. Its most basic use is to calculate the possible different sequences – permutations – n of individual objects: n are!
The factorial function can also be extended to non-integer arguments by defining X while retaining its most important properties! = = (X + 1), where ma is the gamma function; It is undefined when x is a negative integer.
Algorithm to find factorial of a number
- Phase 1. Start
- Phase 2. Read the variable n and read n.
- step 3. Preliminary Facts 1, i to 1.
- step 4. While in loop performance
- Facts = Facts * I
- i = i + 1
- Step 5. print factorial of n.
- Step6. End
Factorial Number Program
// find factorial of a number
n=int(input("enter the n:"))
fact=1
i=1
while n>=i:
fact=fact*i
i=i+1
print(f"factorial of {n} is:",fact)
Output:
>>>
enter the n:15
factorial of 15 is: 1307674368000
>>
FAQ:
How do you find the factorial of a number in Python?
To find the factorial of a number in python you have to follow this code but you can do it in a different way as well.
n=10
fact=1
i=1
while n>=i:
fact=fact*i
i=i+1
print(fact)
How do you find the factorial of a number in a for loop in Python?
To find the factorial of a number in python using for loop i.e.
num = int(input(“Enter A Number: “))
if(num<=0):
print(“Invalid number”)
fact = 1
for i in range(num):
fact = fact * num
num-=1
while(num==0):
print(fact)
break
Recommended Posts:
- How to Become a Machine Learning Engineers – easy Step by Step Guide
- Python Difference Between Two Lists using Python Set and Without Set
- How to Open file in python|Read File Line by Line with Examples
- Python String Find() Method Explained with Examples
- How to Generate Random Numbers in Python with Examples
Get Salesforce Answers