Skip to content
Related Articles

Related Articles

sqrt() function for complex number in C++

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Aug, 2022
View Discussion
Improve Article
Save Article

The complex version of sqrt() function is defined in the complex header file. This function is used to calculate the square root of the complex number z with a branch cut along the negative real axis.

Syntax:  

template <class T> complex<T>
    sqrt(const complex<T>& z);

Parameters: This method takes a mandatory parameter z which represents the complex number whose square root is to be calculated.

Return Value: This function returns the square root of the complex number z.

Below program illustrate the sqrt() function for complex number in C++:

cpp




// C++ program to demonstrate
// example of sqrt() function.
 
#include <complex>
#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
    cout << "Square root of -9 is ";
    cout << sqrt(complex<double>(-9.0, 0.0)) << endl;
 
    cout << "Square root of (-9, -0) is ";
    cout << sqrt(complex<double>(-9.0, -0.0)) << endl;
 
    return 0;
}

Output

Square root of -9 is (0,3)
Square root of (-9, -0) is (0,-3)

Time Complexity: O(√(a2+b2)) where a and b are the real and imaginary part of complex number.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!