Given two complex numbers z1 and z2. The task is to add and subtract the given complex numbers.
Addition of complex number: In Python, complex numbers can be added using + operator.
Examples:
Input: 2+3i, 4+5i
Output: Addition is : 6+8i
Input: 2+3i, 1+2i
Output: Addition is : 3+5i
Example :
Python3
def addComplex( z1, z2):
return z1 + z2
z1 = complex ( 2 , 3 )
z2 = complex ( 1 , 2 )
print ( "Addition is : " , addComplex(z1, z2))
|
Output:
Addition is : (3+5j)
Time Complexity: O(1)
Auxiliary Space: O(1)
Subtraction of complex numbers: Complex numbers in Python can be subtracted using – operator.
Examples:
Input: 2+3i, 4+5i
Output: Subtraction is : -2-2i
Input: 2+3i, 1+2i
Output: Subtraction is : 1+1i
Example :
Python3
def subComplex( z1, z2):
return z1 - z2
z1 = complex ( 2 , 3 )
z2 = complex ( 1 , 2 )
print ( "Subtraction is : " , subComplex(z1, z2))
|
Output:
Subtraction is : (1+1j)
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach#3: Using to different functions
Algorithm
1.Read the two complex numbers from the user.
2.Define a function to add two complex numbers.
3.Define a function to subtract two complex numbers.
4.Call the two functions with the two complex numbers as arguments.
5.Print the results.
Python3
def complex_addition(num1, num2):
return num1 + num2
def complex_subtraction(num1, num2):
return num1 - num2
num1 = complex ( 2 , 3 )
num2 = complex ( 1 , 2 )
addition = complex_addition(num1, num2)
subtraction = complex_subtraction(num1, num2)
print ( "Addition is :" , addition)
print ( "Subtraction is :" , subtraction)
|
Output
Addition is : (3+5j)
Subtraction is : (1+1j)
Time Complexity: O(1) (constant time complexity for addition and subtraction of complex numbers)
Auxiliary Space: O(1) (constant space complexity as we are using only two variables)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!