Factorial Program In Python

Factorial Program In Python

Published on October 21 2021

In this tutorial, you will learn a python program to find the factorial of a given number using for loop and python built in function.

Source code

num = int(input("Enter any number :"))

# Method - 1 --> using loop

def cal_factorial(num):
    factorial = 1
    if num == 0 or num == 1:
        return 1
    for i in range(1, num+1):
        factorial = factorial * i
    return factorial

output = cal_factorial(num)
print('Factorial of number  ', num , ' is : ', output)


# Method - 2 --> using inbuilt function from math module

import math
output = math.factorial(num)
print('Factorial of number  ', num , ' is : ', output)

Video