From a7da52aab078680592e076eed7b6e0d487202483 Mon Sep 17 00:00:00 2001 From: ayushsingh2021 Date: Mon, 21 Oct 2024 04:52:15 +0530 Subject: [PATCH] Create A_factorial.py --- Python/A_factorial.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Python/A_factorial.py diff --git a/Python/A_factorial.py b/Python/A_factorial.py new file mode 100644 index 00000000..9a60f611 --- /dev/null +++ b/Python/A_factorial.py @@ -0,0 +1,22 @@ +class FactorialCalculator: + def __init__(self, number): + self.number = number + + def calculate(self): + if self.number < 0: + raise ValueError("Factorial is not defined for negative numbers.") + return self._factorial(self.number) + + def _factorial(self, n): + if n == 0 or n == 1: + return 1 + else: + return n * self._factorial(n - 1) + +try: + num = int(input("Enter a non-negative integer: ")) + calculator = FactorialCalculator(num) + result = calculator.calculate() + print(f"The factorial of {num} is {result}.") +except ValueError as e: + print(e)