From ea2e6e2b5f7555efbef99c08c1e4bfdcdeebdc57 Mon Sep 17 00:00:00 2001 From: Dhrubaraj Pati Date: Sat, 25 Oct 2025 21:40:36 +0530 Subject: [PATCH] Implement Calculator class with basic operations Calculator class me 4 methods hain: add, subtract, multiply, aur divide. Har method do numbers (a aur b) leta hai aur corresponding result return karta hai. Division method me zero se divide karne par error handle kiya gaya hai. --- 14_OOPS/calculator.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 14_OOPS/calculator.py diff --git a/14_OOPS/calculator.py b/14_OOPS/calculator.py new file mode 100644 index 0000000..7e454a2 --- /dev/null +++ b/14_OOPS/calculator.py @@ -0,0 +1,30 @@ +# Class with Method Task: Create a class Calculator with methods add, subtract, multiply, and divide. Create an object and call each method with example numbers. + +class Calculator: + # Method for addition + def add(self, a, b): + return a + b + + # Method for subtraction + def subtract(self, a, b): + return a - b + + # Method for multiplication + def multiply(self, a, b): + return a * b + + # Method for division + def divide(self, a, b): + if b == 0: + return "Error! Division by zero." + return a / b + + +# Create an object of Calculator class +calc = Calculator() + +# Example calls +print("Addition:", calc.add(10, 5)) +print("Subtraction:", calc.subtract(10, 5)) +print("Multiplication:", calc.multiply(10, 5)) +print("Division:", calc.divide(10, 5))