Square root of two Complex Numbers | Java 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
import java . util . * ; class GFG { static class pair { double first , second ; public pair ( double first , double second ) { this . first = first ; this . second = second ; } } static void complexRoot ( int A , int B ) { Vector < pair > ans = new Vector < pair > ( ) ; double X1 = Math . abs ( Math . sqrt ( ( A + Math . sqrt ( A * A + B * B ) ) / 2 ) ) ; double Y1 = B / ( 2 * X1 ) ; ans . add ( new pair ( X1 , Y1 ) ) ; double X2 = - 1 * X1 ; double Y2 = B / ( 2 * X2 ) ; if ( X2 != 0 ) { ans . add ( new pair ( X2 , Y2 ) ) ; } double X3 = ( A - Math . sqrt ( A * A + B * B ) ) / 2 ; if ( X3 > 0 ) { X3 = Math . abs ( Math . sqrt ( X3 ) ) ; double Y3 = B / ( 2 * X3 ) ; ans . add ( new pair ( X3 , Y3 ) ) ; double X4 = - 1 * X3 ; double Y4 = B / ( 2 * X4 ) ; if ( X4 != 0 ) { ans . add ( new pair ( X4 , Y4 ) ) ; } } System . out . print ( " The ▁ Square ▁ roots ▁ are : ▁ " + "NEW_LINE"); for ( pair p : ans ) { System . out . printf ( " % .4f " , p . first ) ; if ( p . second > 0 ) System . out . print ( " + " ) ; if ( p . second != 0 ) System . out . printf ( "%.4f*iNEW_LINE", p.second); else System . out . println ( ) ; } } public static void main ( String [ ] args ) { int A = 0 , B = 1 ; complexRoot ( A , B ) ; } }