Modulus of a Complex Number | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findModulo ( string s ) { int l = s . length ( ) ; int i , modulus = 0 ; if ( s . find ( ' + ' ) < l ) { i = s . find ( ' + ' ) ; } else { i = s . find ( ' - ' ) ; } string real = s . substr ( 0 , i ) ; string imaginary = s . substr ( i + 1 , l - 1 ) ; int x = stoi ( real ) ; int y = stoi ( imaginary ) ; cout << sqrt ( x * x + y * y ) << " STRNEWLINE " ; } int main ( ) { string s = "3 + 4i " ; findModulo ( s ) ; return 0 ; }