Multiplication of two complex numbers given as strings | PHP program to multiply two complex numbers given as strings . ; Spiting the real and imaginary parts of the given complex strings based on ' + ' and ' i ' symbols . ; Storing the real part of complex string a ; Storing the imaginary part of complex string a ; Storing the real part of complex string b ; Storing the imaginary part of complex string b ; Returns the product . ; Driver Code
< ? php function complexNumberMultiply ( $ a , $ b ) { $ x = preg_split ( " / [ \s + ] + ▁ i / " , $ a ) ; $ y = preg_split ( " / [ \s + ] + ▁ i / " , $ b ) ; $ a_real = intval ( $ x [ 0 ] ) ; $ a_img = intval ( $ x [ 1 ] ) ; $ b_real = intval ( $ y [ 0 ] ) ; $ b_img = intval ( $ y [ 1 ] ) ; return ( $ a_real * $ b_real - $ a_img * $ b_img ) . " + " ( $ a_real * $ b_img + $ a_img * $ b_real ) . " i " ; } $ str1 = "1 + 1i " ; $ str2 = "1 + 1i " ; echo complexNumberMultiply ( $ str1 , $ str2 ) ; ? >