Program to Subtract two Complex Numbers

Python Program to Subtract two Complex Numbers

In the previous article, we have discussed Python Program to Count the Number of Null elements in a List.

Complex numbers in python:

A complex number is formed by combining two real numbers. Python complex numbers can be generated using two methods: the complex() function and a direct assignment statement. Complex numbers are most commonly used when two real numbers are used to define something.

Given two complex numbers, and the task is to subtract given two complex numbers and get the result.

Example:

given complex no = complex(2, 3)   # result  = 2+3j

Examples:

Example1:

Input:

Given first complex number = 2+3j
Given second complex number = 1+5j

Output:

The Subtraction of  (2+3j) - (1+5j) = (1-2j)

Example2:

Input:

Given first complex number = 6+1j
Given second complex number = 5+2j

Output:

The Subtraction of  (6+1j) - (5+2j) = (1-1j)

Program to Subtract two Complex Numbers

Below are the ways to Subtract given two Complex Numbers.

Method #1: Using complex() function  (Static input)

Approach:

  • Give the first complex number as static input and store it in a variable.
  • Give the second complex number as static input and store it in another variable.
  • Subtract the given two complex numbers and store it in another variable.
  • Print the Subtraction of the given two complex numbers
  • The Exit of the program.

Below is the implementation:

# Give the first complex number as static input and store it in a variable.
fst_complx_no = 2 + 3j
# Give the second complex number as static input and store it in another variable.
scnd_complx_no = 1 + 5j
# Subtract the given two complex numbers and store it in another variable.
subtrctn_res = fst_complx_no - scnd_complx_no
# Print the Subtraction of the given two complex numbers
print(" The Subtraction of ", fst_complx_no,
      "-", scnd_complx_no, "=", subtrctn_res)

Output:

The Subtraction of  (2+3j) - (1+5j) = (1-2j)

Method #2: Using complex() function  (User input)

Approach:

  • Give the real part and imaginary part of the first complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store it in a variable.
  • Give the real part and imaginary part of the second complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store it in another variable.
  • Subtract the given two complex numbers and store it in another variable.
  • Print the Subtraction of the given two complex numbers
  • The Exit of the program.

Below is the implementation:

#Give the real part and imaginary part of the first complex number as user input using map(), int(), split().
#Store it in two variables.
realpart_1, imaginarypart_1 = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
#Using a complex() function convert those two variables into a complex number and store it in a variable.   
fst_complexnumbr = complex(realpart_1, imaginarypart_1)
#Give the real part and imaginary part of the second complex number as user input using map(), int(), split().
#Store it in two variables.
realpart_2, imaginarypart_2 = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
#Using a complex() function convert those two variables into a complex number and store it in another variable.
scnd_complexnumbr = complex(realpart_2, imaginarypart_2)
# Subtract the given two complex numbers and store it in another variable.
subtrctn_res = fst_complexnumbr - scnd_complexnumbr
# Print the Subtraction of the given two complex numbers
print(" The Subtraction of ", fst_complexnumbr,
      "-", scnd_complexnumbr, "=", subtrctn_res)

Output:

Enter real part and complex part of the complex number = 3 7
Enter real part and complex part of the complex number = 1 14
The Subtraction of (3+7j) - (1+14j) = (2-7j)

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.