How to calculate factorial of a number in Python

2 Answers

0 votes
def factorial(n):
    fact = 1
    for i in range(2, n + 1):
        fact = fact * i
    return fact

n = 5
print(factorial(n))

n = 4
print(factorial(n))


'''
run:

120
24

'''

 



answered May 9, 2017 by avibootz
0 votes
import math

print(math.factorial(6))
print(math.factorial(5))
print(math.factorial(4))


'''
run:

720
120
24

'''

 



answered Oct 13, 2017 by avibootz

Related questions

1 answer 158 views
1 answer 170 views
1 answer 218 views
1 answer 154 views
1 answer 181 views
2 answers 217 views
...