Python Program to find factorial of a number using class


In this program, you will learn how to find factorial of a number using class in Python.


3! = 1 * 2 * 3 => 6

4! = 1 * 2 * 3 * 4 => 24

Example: How to find factorial of a number using class in Python

class Test:

    def fact(self, n):
        f = 1
        for i in range(1, n + 1):
            f = f * i
        return f


n = int(input("Enter a number:"))

obj = Test()
f = obj.fact(n)
print("Factorial is:", f)

Output:

Enter a number:4
Factorial is: 24