Modulus of a Complex Number | Java program to find the Modulus of a Complex Number ; Function to find modulus of a complex number ; Storing the index of ' + ' ; Storing the index of ' - ' ; Finding the real part of the complex number ; Finding the imaginary part of the complex number ; Driver code
import java . util . * ; class GFG { static void findModulo ( String s ) { int l = s . length ( ) ; int i , modulus = 0 ; if ( s . contains ( " + " ) ) { i = s . indexOf ( " + " ) ; } else { i = s . indexOf ( " - " ) ; } String real = s . substring ( 0 , i ) ; String imaginary = s . substring ( i + 1 , l - 1 ) ; int x = Integer . parseInt ( real ) ; int y = Integer . parseInt ( imaginary ) ; System . out . print ( Math . sqrt ( x * x + y * y ) + "NEW_LINE"); } public static void main ( String [ ] args ) { String s = "3 + 4i " ; findModulo ( s ) ; } }