Skip to content
Related Articles

Related Articles

Improve Article

sqrt() function for complex number in C++

  • Last Updated : 30 Oct, 2020
Geek Week

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 <math.h>
#include <iostream>
#include <complex>
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)
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :