Square root of two Complex Numbers (original) (raw)

Last Updated : 23 Jul, 2025

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:

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 <bits/stdc++.h> using namespace std;

// Function to find the square root of // a complex number void complexRoot(int A, int B) { // Stores all the square roots vector<pair<double, double> > 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 Code int main() { int A = 0, B = 1; complexRoot(A, B);

return 0;

}

Java

// Java program for the above approach import 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 number static void complexRoot(int A, int B) {

// Stores all the square roots
Vector<pair> ans = new Vector<pair>();

// 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 Code public 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 approach

from math import sqrt

Function to find the square root of

a complex number

def 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 Code

if name == 'main':

A,B = 0, 1
complexRoot(A, B)

This code is contributed by mohit kumar 29

C#

// C# code to implement the approach using System; using System.Collections.Generic;

class GFG { // Definition of Pair struct private struct Pair { public double First; public double Second;

// Constructor
public Pair(double first, double second)
{
  First = first;
  Second = second;
}

}

// Function to find the square root of // a complex number private static void ComplexRoot(int A, int B) { // Stores all the square roots var ans = new List();

// 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
Console.WriteLine("The Square roots are: \n");

foreach (var p in ans)
{
  Console.Write($"{p.First:0.000000}");

  if (p.Second > 0)
  {
    Console.Write("+");
  }

  if (p.Second != 0)
  {
    Console.Write($"{p.Second:0.000000} * i\n");
  }
  else
  {
    Console.WriteLine();
  }
}

}

// Driver code static void Main(string[] args) { int A = 0, B = 1;

ComplexRoot(A, B);

} }

// This code is contributed by phasing17

JavaScript

`

Output:

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

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