sqrt() function for complex number in C++

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 mandaory 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++:

Example:-

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.