Square root of two Complex Numbers | C ++ program for the above approach ; Function to find the square root of a complex number ; Stores all the square roots ; Stores the first square root ; Push the square root in the ans ; Stores the second square root ; If X2 is not 0 ; Push the square root in the array ans [ ] ; Stores the third square root ; If X3 is greater than 0 ; Push the square root in the array ans [ ] ; Stores the fourth square root ; Push the square root in the array ans [ ] ; Prints the square roots ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void complexRoot ( int A , int B ) { vector < pair < double , double > > ans ; double X1 = abs ( sqrt ( ( A + sqrt ( A * A + B * B ) ) / 2 ) ) ; double Y1 = B / ( 2 * X1 ) ; ans . push_back ( { X1 , Y1 } ) ; double X2 = -1 * X1 ; double Y2 = B / ( 2 * X2 ) ; if ( X2 != 0 ) { ans . push_back ( { X2 , Y2 } ) ; } double X3 = ( A - sqrt ( A * A + B * B ) ) / 2 ; if ( X3 > 0 ) { X3 = abs ( sqrt ( X3 ) ) ; double Y3 = B / ( 2 * X3 ) ; ans . push_back ( { X3 , Y3 } ) ; double X4 = -1 * X3 ; double Y4 = B / ( 2 * X4 ) ; if ( X4 != 0 ) { ans . push_back ( { X4 , Y4 } ) ; } } 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 ; } } int main ( ) { int A = 0 , B = 1 ; complexRoot ( A , B ) ; return 0 ; }