-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ86.py
More file actions
33 lines (30 loc) · 1.33 KB
/
Copy pathQ86.py
File metadata and controls
33 lines (30 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class ComplexNumber:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __repr__(self):
return f"{self.real} + {self.imaginary}i"
def __add__(self, other):
if isinstance(other, ComplexNumber):
real_part = self.real + other.real
imaginary_part = self.imaginary + other.imaginary
return ComplexNumber(real_part, imaginary_part)
return NotImplemented
def __sub__(self, other):
if isinstance(other, ComplexNumber):
real_part = self.real - other.real
imaginary_part = self.imaginary - other.imaginary
return ComplexNumber(real_part, imaginary_part)
return NotImplemented
real1 = float(input("Enter the real part of the first complex number: "))
imaginary1 = float(input("Enter the imaginary part of the first complex number: "))
real2 = float(input("Enter the real part of the second complex number: "))
imaginary2 = float(input("Enter the imaginary part of the second complex number: "))
complex1 = ComplexNumber(real1, imaginary1)
complex2 = ComplexNumber(real2, imaginary2)
sum_result = complex1 + complex2
difference_result = complex1 - complex2
print(f"Complex 1: {complex1}")
print(f"Complex 2: {complex2}")
print(f"Sum: {sum_result}")
print(f"Difference: {difference_result}")