Write a python program to add and multiply complex numbers

 # Function to add complex numbers

def add_complex(c1, c2):

    return complex(c1.real + c2.real, c1.imag + c2.imag)


# Function to multiply complex numbers

def multiply_complex(c1, c2):

    real = c1.real * c2.real - c1.imag * c2.imag

    imag = c1.real * c2.imag + c1.imag * c2.real

    return complex(real, imag)


# Input from user

real1 = float(input("Enter real part of first complex number: "))

imag1 = float(input("Enter imaginary part of first complex number: "))

real2 = float(input("Enter real part of second complex number: "))

imag2 = float(input("Enter imaginary part of second complex number: "))


# Creating complex numbers

c1 = complex(real1, imag1)

c2 = complex(real2, imag2)


# Performing operations

sum_result = add_complex(c1, c2)

product_result = multiply_complex(c1, c2)


# Displaying results

print(f"Sum of complex numbers: {sum_result}")

print(f"Product of complex numbers: {product_result}")

Output:


Comments