Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 14_OOPS/calculator.py
Original file line number Diff line number Diff line change
@@ -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))