Given two Complex Numbers Z1 and Z2 in the Cartesian form, the task is to convert the given complex number into polar form and perform all the arithmetic operations ( addition, subtraction, multiplication, and division ) on them.
Examples:
Input: Z1 = (2, 3), Z2 = (4, 6)
Output:
Polar form of the first Complex Number: (3.605551275463989, 0.9827937232473292)
Polar form of the Second Complex Number: (7.211102550927978, 0.9827937232473292)
Addition of two Complex Numbers: (10.816653826391967, 0.9827937232473292)
Subtraction of two Complex Numbers: (3.605551275463989, -0.9827937232473292)
Multiplication of two Complex Numbers: (25.999999999999996, 1.9655874464946583)
Division of two Complex Numbers: (0.5, 0.0)Input: Z1 = (1, 1), Z2 = (2, 2)
Output:
Polar form of the first Complex Number: (1.4142135623730951, 0.7853981633974482)
Polar form of the Second Complex Number: (2.8284271247461903, 0.7853981633974482)
Addition of two Complex Numbers: (4.242640687119286, 0.7853981633974482)
Subtraction of two Complex Numbers: (1.4142135623730951, -0.7853981633974482)
Multiplication of two Complex Numbers: (4.000000000000001, 1.5707963267948963)
Division of two Complex Numbers: (0.5, 0.0)
Approach: The given problem can be solved based on the following properties of Complex Numbers:
- A complex number Z in Cartesian form is represented as:
,Â
where a, b € R and b is known as the imaginary part of the complex number andÂ
- The polar form of complex number Z is:
where, r is known as modules of a complex number and
 is the angle made with the positive X axis.
- In the expression of complex number in polar form taking r as common performingÂ
 the expression turn into:
, which is known as the Eulerian form of the Complex Number.
- The eulerian and polar forms both are represented as:Â
.
- The multiplication and divisions of two complex numbers can be done using the eulerian form:
For Multiplication:
   Â
=>ÂFor Division:
   Â
=>Â
Follow the steps below to solve the problem:
- Convert the complex numbers into polar using the formula discussed-above and print it in the form forÂ
.
- Define a function say Addition(Z1, Z2) to perform addition operation:
- Find the real part of the complex number by adding two real parts Z1 and Z2, and store it in a variable say a.
- Find the imaginary part of the complex number by adding two imaginary parts of the complex numbers Z1 and Z2 and store it in a variable say b.
- Convert the Cartesian form of the complex to polar form and print it.
- Define a function say Subtraction(Z1, Z2) to perform subtraction operation:
- Find the real part of the complex number by subtracting two real parts Z1 and Z2, and store it in a variable say a.
- Find the imaginary part of the complex number by subtracting two imaginary parts of the complex numbers Z1 and Z2 and store it in a variable say b.
- Convert the Cartesian form of the complex to polar form and print it.
- Print the multiplication of two complex number Z1 and Z2 asÂ
- Print the Division of two complex number Z1 and Z2 asÂ
Below is the implementation of the above approach:
C++
#include <iostream>#include <cmath>Â
using namespace std;Â
// Function to find the polar form// of the given Complex Numberpair<double, double> get_polar_form(double re, double im){Â Â Â Â // Z is in cartesian formÂ
    // Stores the modulo of complex number    double r = sqrt(re * re + im * im);         // If r is greater than 0    if(r){        double theta = asin(im / r);        return make_pair(r, theta);    }Â
    // Otherwise    else{        return make_pair(0, 0);    }}   Â
// Function to add two complex numberspair<double, double> Addition(pair<double, double> z1, pair<double, double> z2){    // Z is in polar form    double r1 = z1.first;    double theta1 = z1.second;    double r2 = z2.first;    double theta2 = z2.second;Â
    // Real part of complex number    double a = r1 * cos(theta1) + r2 * cos(theta2);         // Imaginary part of complex Number    double b = r1 * sin(theta1) + r2 * sin(theta2);Â
    // Find the polar form    return get_polar_form(a, b);}   Â
Â
// Function to subtract two// given complex numberspair<double, double> Subtraction(pair<double, double> z1, pair<double, double> z2){     // Z is in polar form    double r1 = z1.first;    double theta1 = z1.second;    double r2 = z2.first;    double theta2 = z2.second;Â
    // Real part of the complex number    double a = r1 * cos(theta1) - r2 * cos(theta2);         // Imaginary part of complex number    double b = r1 * sin(theta1) - r2 * sin(theta2);Â
    // Converts (a, b) to polar    // form and return    return get_polar_form(a, b); }Â
Â
// Function to multiply two complex numberspair<double, double> Multiplication(pair<double, double> z1, pair<double, double> z2){    // z is in polar form    double r1 = z1.first;    double theta1 = z1.second;    double r2 = z2.first;    double theta2 = z2.second;Â
    // Return the multiplication of Z1 and Z2    return make_pair(r1 * r2, theta1 + theta2);}Â
// Function to divide two complex numberspair<double, double> Division(pair<double, double> z1, pair<double, double> z2){    // Z is in the polar form    double r1 = z1.first;    double theta1 = z1.second;    double r2 = z2.first;    double theta2 = z2.second;         // Return the division of Z1 and Z2    return make_pair(r1 / r2, theta1 - theta2);}Â
// Driver Codeint main() {Â Â Â Â pair<double, double> z1 = make_pair(2.0, 3.0);Â Â Â Â pair<double, double> z2 = make_pair(4.0, 6.0);Â
    // Convert into Polar Form    auto z1_polar = get_polar_form(z1.first, z1.second);    auto z2_polar = get_polar_form(z2.first, z2.second);Â
    cout << "Polar form of the first Complex Number: " << z1_polar.first << " " << z1_polar.second << endl;    cout << "Polar form of the Second Complex Number: " << z2_polar.first << " " << z2_polar.second << endl;Â
    cout << "Addition of two Complex Numbers: " << Addition(z1_polar, z2_polar).first << " " << Addition(z1_polar, z2_polar).second << endl;Â
    cout << "Subtraction of two Complex Numbers: " << Subtraction(z1_polar, z2_polar).first << " " << Subtraction(z1_polar, z2_polar).second << endl;Â
    cout << "Multiplication of two Complex Numbers: " << Multiplication(z1_polar, z2_polar).first << " " << Multiplication(z1_polar, z2_polar).second << endl;Â
    cout << "Division of two Complex Numbers: " << Division(z1_polar, z2_polar).first << " " << Division(z1_polar, z2_polar).second << endl;Â
    return 0;}Â
// This code is contributed by phasing17. |
Java
import java.util.*;Â
class GFG {Â
  // Function to find the polar form of the given Complex Number  public static double[] GetPolarForm(double re, double im) {    // Z is in cartesian formÂ
    // Stores the modulo of complex number    double r = Math.sqrt(re * re + im * im);Â
    // If r is greater than 0    if (r > 0) {      double theta = Math.asin(im / r);      return new double[] {r, theta};    }Â
    // Otherwise    else {      return new double[] {0.00, 0.00};    }  }Â
  // Function to add two complex numbers  public static double[] Addition(double[] z1, double[] z2) {    // Z is in polar form    double r1 = z1[0];    double theta1 = z1[1];    double r2 = z2[0];    double theta2 = z2[1];Â
    // Real part of complex number    double a = r1 * Math.cos(theta1) + r2 * Math.cos(theta2);Â
    // Imaginary part of complex Number    double b = r1 * Math.sin(theta1) + r2 * Math.sin(theta2);Â
    // Find the polar form    return GetPolarForm(a, b);  }Â
  // Function to subtract two given complex numbers  public static double[] Subtraction(double[] z1, double[] z2) {    // Z is in polar form    double r1 = z1[0];    double theta1 = z1[1];    double r2 = z2[0];    double theta2 = z2[1];Â
    // Real part of the complex number    double a = r1 * Math.cos(theta1) - r2 * Math.cos(theta2);Â
    // Imaginary part of complex number    double b = r1 * Math.sin(theta1) - r2 * Math.sin(theta2);Â
    // Converts (a, b) to polar form and return    return GetPolarForm(a, b);  }Â
  // Function to multiply two complex numbers  public static double[] Multiplication(double[] z1, double[] z2) {    // z is in polar form    double r1 = z1[0];    double theta1 = z1[1];    double r2 = z2[0];    double theta2 = z2[1];Â
    // Return the multiplication of Z1 and Z2    return new double[] {r1 * r2, theta1 + theta2};  }Â
  // Function to divide two complex numbers  public static double[] Division(double[] z1, double[] z2) {    // Z is in the polar form    double r1 = z1[0];    double theta1 = z1[1];    double r2 = z2[0];    double theta2 = z2[1];Â
    // Return the division of Z1 and Z2    return new double[] {r1 / r2, theta1 - theta2};  }Â
  public static void main(String[] args) {    double[] z1 = {2.0, 3.0};    double[] z2 = {4.0, 6.0};Â
    // Convert into Polar Form    var z1_polar = GetPolarForm(z1[0], z1[1]);    var z2_polar = GetPolarForm(z2[0], z2[1]);Â
    System.out.println(      "Polar form of the first Complex Number: "      + z1_polar[0] + " " + z1_polar[1]);    System.out.println(      "Polar form of the Second Complex Number: "      + z2_polar[0] + " " + z2_polar[1]);Â
    System.out.println(      "Addition of two Complex Numbers: "      + Arrays.toString(Addition(z1_polar, z2_polar)));    System.out.println(      "Subtraction of two Complex Numbers: "      + Arrays.toString(Subtraction(z1_polar, z2_polar)));    System.out.println(      "Multiplication of two Complex Numbers: "      + Arrays.toString(Multiplication(z1_polar, z2_polar)));    System.out.println(      "Division of two Complex Numbers: "      + Arrays.toString(Division(z1_polar, z2_polar)));  }}Â
// This code is contributed by phasing17. |
Python3
# Python program for the above approachimport mathÂ
# Function to find the polar form# of the given Complex Numberdef get_polar_form(z):       # Z is in cartesian form    re, im = zÂ
    # Stores the modulo of complex number    r = (re * re + im * im) ** 0.5Â
    # If r is greater than 0    if r:        theta = math.asin(im / r)        return (r, theta)           # Otherwise    else:        return (0, 0)Â
# Function to add two complex numbersdef Addition(z1, z2):       # Z is in polar form    r1, theta1 = z1    r2, theta2 = z2Â
    # Real part of complex number    a = r1 * math.cos(theta1) + r2 * math.cos(theta2)         # Imaginary part of complex Number    b = r1 * math.sin(theta1) + r2 * math.sin(theta2)         # Find the polar form    return get_polar_form((a, b))Â
# Function to subtract two# given complex numbersdef Subtraction(z1, z2):       # Z is in polar form    r1, theta1 = z1    r2, theta2 = z2Â
    # Real part of the complex number    a = r1 * math.cos(theta1) - r2 * math.cos(theta2)         # Imaginary part of complex number    b = r1 * math.sin(theta1) - r2 * math.sin(theta2)Â
    # Converts (a, b) to polar    # form and return    return get_polar_form((a, b))Â
# Function to multiply two complex numbersdef Multiplication(z1, z2):       # z is in polar form    r1, theta1 = z1    r2, theta2 = z2Â
    # Return the multiplication of Z1 and Z2    return (r1 * r2, theta1 + theta2)Â
Â
# Function to divide two complex numbersdef Division(z1, z2):       # Z is in the polar form    r1, theta1 = z1    r2, theta2 = z2Â
    # Return the division of Z1 and Z2    return (r1 / r2, theta1-theta2)Â
Â
# Driver Codeif __name__ == "__main__":Â Â Â Â Â Â Â z1 = (2, 3)Â Â Â Â z2 = (4, 6)Â
    # Convert into Polar Form    z1_polar = get_polar_form(z1)    z2_polar = get_polar_form(z2)Â
    print("Polar form of the first")    print("Complex Number: ", z1_polar)    print("Polar form of the Second")    print("Complex Number: ", z2_polar)Â
    print("Addition of two complex")    print("Numbers: ", Addition(z1_polar, z2_polar))         print("Subtraction of two ")    print("complex Numbers: ",           Subtraction(z1_polar, z2_polar))         print("Multiplication of two ")    print("Complex Numbers: ",           Multiplication(z1_polar, z2_polar))               print("Division of two complex ")    print("Numbers: ", Division(z1_polar, z2_polar)) |
Javascript
// JavaScript program for the above approachÂ
// Function to find the polar form// of the given Complex Numberfunction get_polar_form(z){    // Z is in cartesian form    let re = z[0];    let im = z[1];Â
    // Stores the modulo of complex number    let r = (re * re + im * im) ** 0.5;         // If r is greater than 0    if(r){        let theta = Math.asin(im / r);        return [r, theta];    }Â
    // Otherwise    else{        return [0, 0];    }         }   Â
// Function to add two complex numbersfunction Addition(z1, z2){    // Z is in polar form    let r1 = z1[0];    let theta1 = z1[1];    let r2 = z2[0];    let theta2 = z2[1];Â
    // console.log(r1, r2, theta1, theta2)    // Real part of complex number    let a = r1 * Math.cos(theta1) + r2 * Math.cos(theta2);         // Imaginary part of complex Number    let b = r1 * Math.sin(theta1) + r2 * Math.sin(theta2);    console.log(a, b)    // Find the polar form    return get_polar_form([a, b]);}   Â
Â
// Function to subtract two// given complex numbersfunction Subtraction(z1, z2){     // Z is in polar form    let r1 = z1[0];    let theta1 = z1[1];    let r2 = z2[0];    let theta2 = z2[1];Â
    // Real part of the complex number    let a = r1 * Math.cos(theta1) - r2 * Math.cos(theta2);         // Imaginary part of complex number    let b = r1 * Math.sin(theta1) - r2 * Math.sin(theta2);Â
    // Converts (a, b) to polar    // form and return    return get_polar_form([a, b]); }Â
Â
// Function to multiply two complex numbersfunction Multiplication(z1, z2){    // z is in polar form    let r1 = z1[0];    let theta1 = z1[1];    let r2 = z2[0];    let theta2 = z2[1];Â
    // Return the multiplication of Z1 and Z2    return [r1 * r2, theta1 + theta2];}Â
// Function to divide two complex numbersfunction Division(z1, z2){    // Z is in the polar form    let r1 = z1[0];    let theta1 = z1[1];    let r2 = z2[0];    let theta2 = z2[1];Â
    // Return the division of Z1 and Z2    return [r1 / r2, theta1-theta2];}   Â
// Driver Codez1 = [2, 3];z2 = [4, 6];Â
// Convert into Polar Formz1_polar = get_polar_form(z1)z2_polar = get_polar_form(z2)Â
console.log("Polar form of the first");console.log("Complex Number: ", z1_polar);console.log("Polar form of the Second");console.log("Complex Number: ", z2_polar);Â
console.log("Addition of two complex");console.log("Numbers: ", Addition(z1_polar, z2_polar));Â
console.log("Subtraction of two ");console.log("complex Numbers: ",       Subtraction(z1_polar, z2_polar));Â
console.log("Multiplication of two ");console.log("Complex Numbers: ",       Multiplication(z1_polar, z2_polar));Â
console.log("Division of two complex ");console.log("Numbers: ", Division(z1_polar, z2_polar));Â
// The code is contributed by Gautam goel (gautamgoel962) |
C#
// C# code to implement the approachÂ
using System;using System.Collections.Generic;Â
class GFG {    // Function to find the polar form of the given Complex    // Number    public static Tuple<double, double>    GetPolarForm(double re, double im)    {        // Z is in cartesian formÂ
        // Stores the modulo of complex number        double r = Math.Sqrt(re * re + im * im);Â
        // If r is greater than 0        if (r > 0) {            double theta = Math.Asin(im / r);            return Tuple.Create(r, theta);        }Â
        // Otherwise        else {            return Tuple.Create(0.00, 0.00);        }    }Â
    // Function to add two complex numbers    public static Tuple<double, double>    Addition(Tuple<double, double> z1,             Tuple<double, double> z2)    {        // Z is in polar form        double r1 = z1.Item1;        double theta1 = z1.Item2;        double r2 = z2.Item1;        double theta2 = z2.Item2;Â
        // Real part of complex number        double a            = r1 * Math.Cos(theta1) + r2 * Math.Cos(theta2);Â
        // Imaginary part of complex Number        double b            = r1 * Math.Sin(theta1) + r2 * Math.Sin(theta2);Â
        // Find the polar form        return GetPolarForm(a, b);    }Â
    // Function to subtract two given complex numbers    public static Tuple<double, double>    Subtraction(Tuple<double, double> z1,                Tuple<double, double> z2)    {        // Z is in polar form        double r1 = z1.Item1;        double theta1 = z1.Item2;        double r2 = z2.Item1;        double theta2 = z2.Item2;Â
        // Real part of the complex number        double a            = r1 * Math.Cos(theta1) - r2 * Math.Cos(theta2);Â
        // Imaginary part of complex number        double b            = r1 * Math.Sin(theta1) - r2 * Math.Sin(theta2);Â
        // Converts (a, b) to polar form and return        return GetPolarForm(a, b);    }Â
    // Function to multiply two complex numbers    public static Tuple<double, double>    Multiplication(Tuple<double, double> z1,                   Tuple<double, double> z2)    {        // z is in polar form        double r1 = z1.Item1;        double theta1 = z1.Item2;        double r2 = z2.Item1;        double theta2 = z2.Item2;Â
        // Return the multiplication of Z1 and Z2        return Tuple.Create(r1 * r2, theta1 + theta2);    }Â
    // Function to divide two complex numbers    public static Tuple<double, double>    Division(Tuple<double, double> z1,             Tuple<double, double> z2)    {        // Z is in the polar form        double r1 = z1.Item1;        double theta1 = z1.Item2;        double r2 = z2.Item1;        double theta2 = z2.Item2;Â
        // Return the division of Z1 and Z2        return Tuple.Create(r1 / r2, theta1 - theta2);    }Â
    // Driver Code    public static void Main(string[] args)    {        Tuple<double, double> z1 = Tuple.Create(2.0, 3.0);        Tuple<double, double> z2 = Tuple.Create(4.0, 6.0);Â
        // Convert into Polar Form        var z1_polar = GetPolarForm(z1.Item1, z1.Item2);        var z2_polar = GetPolarForm(z2.Item1, z2.Item2);Â
        Console.WriteLine(            "Polar form of the first Complex Number: "            + z1_polar.Item1 + " " + z1_polar.Item2);        Console.WriteLine(            "Polar form of the Second Complex Number: "            + z2_polar.Item1 + " " + z2_polar.Item2);Â
        Console.WriteLine(            "Addition of two Complex Numbers: "            + Addition(z1_polar, z2_polar));        Console.WriteLine(            "Subtraction of two Complex Numbers: "            + Subtraction(z1_polar, z2_polar));        Console.WriteLine(            "Multiplication of two Complex Numbers: "            + Multiplication(z1_polar, z2_polar));        Console.WriteLine(            "Division of two Complex Numbers: "            + Division(z1_polar, z2_polar));    }}Â
// This code is contributed by phasing17 |
Polar form of the first Complex Number: (3.605551275463989, 0.9827937232473292) Polar form of the Second Complex Number: (7.211102550927978, 0.9827937232473292) Addition of two complex Numbers: (10.816653826391967, 0.9827937232473292) Subtraction of two complex Numbers: (3.605551275463989, -0.9827937232473292) Multiplication of two Complex Numbers: (25.999999999999996, 1.9655874464946583) Division of two complex Numbers: (0.5, 0.0)
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
