Skip to content
Related Articles

Related Articles

sqrt() function for complex number in C++
  • Last Updated : 30 Oct, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output

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

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :