Python Program to Divide Two Complex Numbers

In this article, we will learn about python program to divide two complex numbers with examples.

Getting Started

Complex numbers are represented in the form of a+bj where a and b are real numbers. For example, 2+1j, 5-2j etc.

We have already covered on how to

  • add two complex numbers
  • multiply two complex numbers
  • subtract two complex numbers

Now, we will learn about how to divide two complex numbers in python. For example,

If a = 3+4j and b = 5+2j, then a / b = 0.79+0.48j

We have to perform above task in complex numbers using python programming.

We can do so in multiple ways –

Simple Approach

We use division operator (i.e. / operator) to divide two complex number. For example,

 
a = 3+4j
b = 5+2j
result = a / b
print("Result =", result)

Output:

 
Result = (0.793103448275862+0.48275862068965514j)

Using User Input

In above example, two complex numbers were hardcoded values. We can take those complex numbers as input from user as well. For example,

 
a = complex(input("Enter first complex number: "))
b = complex(input("Enter second complex number: "))
result = a / b
print("Result =", result)

Output:

 
Enter first complex number: 
2+2j
Enter second complex number: 
2+2j
Result = (1+0j)

Using Lambda

We can also use lambda and / operator to divide two complex numbers. For example,

 
if __name__ == "__main__" :
    num1 = 10+15j
    num2 = 3+3j
     
    # Divide two complex numbers
    divide_twoNum = lambda num1, num2 : num1 / num2
    print("Result =", divide_twoNum(num1, num2))

Output:

 
Result = (4.166666666666667+0.8333333333333334j)

Using Function

We can write logic to divide two complex numbers inside python function as well. For example,

 
def divideComplex(a, b):
    return a / b
 
print("Result =", divideComplex(20+2j, 10+12j))

Output:

 
Result = (0.9180327868852458-0.901639344262295j)

Using user defined Complex number using Class

We can also define complex number using class. This is used defined complex number wrapped inside class. Similarly, we also handle logic to divide two complex numbers inside a function in class. For example,

 
class ComplexNumber():

    def init(self):
        self.real = int(input("Enter Real part: "))
        self.imaginary = int(input("Enter Imaginary part: "))

    def display(self):
        sign = "+" if(self.imaginary >= 0) else ""
        print(self.real, sign, self.imaginary, "j", sep="")

    def divide(self, c1, c2):
        res = complex(c1.real, c1.imaginary) / complex(c1.real, c1.imaginary)
        self.real = res.real
        self.imaginary = res.imag


print("First complex number: ")
c1 = ComplexNumber()
c1.init()

print("Second complex number: ")
c2 = ComplexNumber()
c2.init()

print("Result = ")
c3 = ComplexNumber()
c3.divide(c1, c2)
c3.display()

Output:

 
First complex number: 
Enter Real part: 
5
Enter Imaginary part: 
5
Second complex number: 
Enter Real part: 
5
Enter Imaginary part: 
5
Result = 
1.0+0.0j

That’s how we can write python program to divide two complex numbers with examples.

Reference: Official Doc

Learn about more python examples at – https://tutorialwing.com/python-tutorial/

Leave a Reply