# Square root of two Complex Numbers

• Last Updated : 14 Jun, 2021

Given two positive integers A and B representing the complex number Z in the form of Z = A + i * B, the task is to find the square root of the given complex number.

Examples:

Input: A = 0, B =1
Output:
The Square roots are:
0.707107 + 0.707107*i
-0.707107 – 0.707107*i

Input: A = 4, B = 0
Output:
The Square roots are:
2
-2

Approach: The given problem can be solved based on the following observations:

• It is known that the square root of a complex number is also a complex number.
• Then considering the square root of the complex number equal to X + i*Y, the value of (A + i*B) can be expressed as:
• A + i * B = (X + i * Y) * (X + i * Y)
• A + i * B = X2 – Y2+ 2 * i * X * Y
• Equating the value of real and complex parts individually:

From the above observations, calculate the value of X and Y using the above formula and print the value (X + i*Y) as the resultant square root value of the given complex number.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to find the square root of// a complex numbervoid complexRoot(int A, int B){    // Stores all the square roots    vector > ans;     // Stores the first square root    double X1 = abs(sqrt((A + sqrt(A * A                                   + B * B))                         / 2));    double Y1 = B / (2 * X1);     // Push the square root in the ans    ans.push_back({ X1, Y1 });     // Stores the second square root    double X2 = -1 * X1;    double Y2 = B / (2 * X2);     // If X2 is not 0    if (X2 != 0) {         // Push the square root in        // the array ans[]        ans.push_back({ X2, Y2 });    }     // Stores the third square root    double X3 = (A - sqrt(A * A + B * B)) / 2;     // If X3 is greater than 0    if (X3 > 0) {        X3 = abs(sqrt(X3));        double Y3 = B / (2 * X3);         // Push the square root in        // the array ans[]        ans.push_back({ X3, Y3 });         // Stores the fourth square root        double X4 = -1 * X3;        double Y4 = B / (2 * X4);         if (X4 != 0) {             // Push the square root            // in the array ans[]            ans.push_back({ X4, Y4 });        }    }     // Prints the square roots    cout << "The Square roots are: "         << endl;     for (auto p : ans) {        cout << p.first;        if (p.second > 0)            cout << "+";        if (p.second)            cout << p.second                 << "*i" << endl;        else            cout << endl;    }} // Driver Codeint main(){    int A = 0, B = 1;    complexRoot(A, B);     return 0;}

## Java

 // Java program for the above approachimport java.util.*; class GFG{     static class pair{    double first, second;    public pair(double first,                double second)     {        this.first = first;        this.second = second;    }   } // Function to find the square root of// a complex numberstatic void complexRoot(int A, int B){         // Stores all the square roots    Vector ans = new Vector();     // Stores the first square root    double X1 = Math.abs(Math.sqrt((A + Math.sqrt(A * A +                                    B * B)) / 2));    double Y1 = B / (2 * X1);     // Push the square root in the ans    ans.add(new pair( X1, Y1 ));     // Stores the second square root    double X2 = -1 * X1;    double Y2 = B / (2 * X2);     // If X2 is not 0    if (X2 != 0)    {                 // Push the square root in        // the array ans[]        ans.add(new pair(X2, Y2));    }     // Stores the third square root    double X3 = (A - Math.sqrt(A * A + B * B)) / 2;     // If X3 is greater than 0    if (X3 > 0)    {        X3 = Math.abs(Math.sqrt(X3));        double Y3 = B / (2 * X3);         // Push the square root in        // the array ans[]        ans.add(new pair(X3, Y3));         // Stores the fourth square root        double X4 = -1 * X3;        double Y4 = B / (2 * X4);         if (X4 != 0)        {                         // Push the square root            // in the array ans[]            ans.add(new pair(X4, Y4));        }    }     // Prints the square roots    System.out.print("The Square roots are: " + "\n");     for(pair p : ans)    {        System.out.printf("%.4f", p.first);        if (p.second > 0)            System.out.print("+");        if (p.second != 0)            System.out.printf("%.4f*i\n", p.second);        else            System.out.println();    }} // Driver Codepublic static void main(String[] args){    int A = 0, B = 1;         complexRoot(A, B);}} // This code is contributed by shikhasingrajput

## Python3

 # Python3 program for the above approachfrom math import sqrt # Function to find the square root of# a complex numberdef complexRoot(A, B):         # Stores all the square roots    ans = []     # Stores the first square root    X1 = abs(sqrt((A + sqrt(A * A + B * B)) / 2))    Y1 = B / (2 * X1)     # Push the square root in the ans    ans.append([X1, Y1])     # Stores the second square root    X2 = -1 * X1    Y2 = B / (2 * X2)     # If X2 is not 0    if (X2 != 0):         # Push the square root in        # the array ans[]        ans.append([X2, Y2])     # Stores the third square root    X3 = (A - sqrt(A * A + B * B)) / 2     # If X3 is greater than 0    if (X3 > 0):        X3 = abs(sqrt(X3))        Y3 = B / (2 * X3)         # Push the square root in        # the array ans[]        ans.append([X3, Y3])         # Stores the fourth square root        X4 = -1 * X3        Y4 = B / (2 * X4)         if (X4 != 0):             # Push the square root            # in the array ans[]            ans.append([X4, Y4])     # Prints the square roots    print("The Square roots are: ")     for p in ans:        print(round(p[0], 6), end = "")        if (p[1] > 0):            print("+", end = "")        if (p[1]):            print(str(round(p[1], 6)) + "*i")        else:            print() # Driver Codeif __name__ == '__main__':         A,B = 0, 1    complexRoot(A, B) # This code is contributed by mohit kumar 29

## Javascript

 

Output:

The Square roots are:
0.707107+0.707107*i
-0.707107-0.707107*i

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up