Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (16.4k points)
In python, how to compute factorial of an integer?

1 Answer

0 votes
by (26.4k points)

Simplest way is to use math.factorial (Which is available from python 2.6 and above)

import math


You can also use an iterative approach:

def factorial(n):

    fact = 1

    for num in range(2, n + 1):

        fact *= num

    return fact

or recursive approach:

def factorial(n):

    if n < 2:

        return 1


        return n * factorial(n-1)

Want to learn the concepts of python in detail? Join python certification course and get certifed 

Related questions

0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
4 answers
0 votes
1 answer

Browse Categories