OKPEDIA PYTHON EN

How to calculate factorial in python

To find the factorial n! of a number with python use the factorial() function of the math library.

import math
math.factorial(n)

Parameter n is a positive integer.

the formula of factorial n!

The function outputs the factorial calculation n! of the number n.

A practical example

Example 1

This script calculates the factorial of 3

>>> import math
>>> math.factorial(3)
6

The factorial of 3! is equal to 1 · 2 · 3 = 6.

Example 2

This script calculates the factorial of 4

>>> import math
>>> n=4
>>> math.factorial(n)
24

The factorial of 4! is equal to 1 · 2 · 3 · 4 = 24

https://how.okpedia.org/en/python/how-to-calculate-factorial-in-python


Report an error or share a suggestion to enhance this page



FacebookTwitterLinkedinLinkedin