Find the real and imaginary part of a Complex number - GeeksforGeeks

19 May, 2021
Given a complex number Z, the task is to determine the real and imaginary parts of this complex number.Examples:  
Input: z = 3 + 4i Output: Real part: 3, Imaginary part: 4Input: z = 6 – 8i Output: Real part: 6, Imaginary part: 8  
 
Approach: A complex number can be represented as Z = x + yi, where x is real part and y is imaginary. We will follow the below steps to separate out real and imaginary part  
Find out the index of + or – operator in the stringReal part will be a substring starting from index 0 to a length (index of operator – 1)Imaginary part will be a substring starting from index (index of operator + 1) to (length of string – index of operator – 2)
Find out the index of + or – operator in the string
Real part will be a substring starting from index 0 to a length (index of operator – 1)
Imaginary part will be a substring starting from index (index of operator + 1) to (length of string – index of operator – 2)
Implementation:  
C++
Java
Python3
C#
Javascript
// C++ program to find the real and// imaginary parts of a Complex Number#include <bits/stdc++.h>using namespace std; // Function to find real and imaginary// parts of a complex numbervoid findRealAndImag(string s){    // string length stored in variable l    int l = s.length();     // variable for the index of the separator    int i;     // Storing the index of '+'    if (s.find('+') < l) {        i = s.find('+');    }    // else storing the index of '-'    else {        i = s.find('-');    }     // Finding the real part    // of the complex number    string real = s.substr(0, i);     // Finding the imaginary part    // of the complex number    string imaginary = s.substr(i + 1, l - i - 2);     cout << "Real part: " << real << "\n";    cout << "Imaginary part: "         << imaginary << "\n";} // Driver codeint main(){    string s = "3+4i";     findRealAndImag(s);     return 0;}
// Java program to find the real and// imaginary parts of a Complex Numberclass GFG{    // Function to find real and imaginary    // parts of a complex number    static void findRealAndImag(String s)    {        // string length stored in variable l        int l = s.length();              // variable for the index of the separator        int i;              // Storing the index of '+'        if (s.indexOf('+') != -1) {            i = s.indexOf('+');        }         // else storing the index of '-'        else {            i = s.indexOf('-');        }               // Finding the real part        // of the complex number        String real = s.substring(0, i);              // Finding the imaginary part        // of the complex number        String imaginary = s.substring(i + 1, l - 1);              System.out.println("Real part: " + real );        System.out.println("Imaginary part: "+              imaginary);    }          // Driver code    public static void main(String []args)    {        String s = "3+4i";              findRealAndImag(s);         }} // This code is contributed by chitranayal
# Python3 program to find the real and# imaginary parts of a Complex Number # Function to find real and imaginary# parts of a complex numberdef findRealAndImag(s) :     # string length stored in variable l    l = len(s)     # variable for the index of the separator    i = 0     # Storing the index of '+'    if (s.find('+') != -1):        i = s.find('+')    # else storing the index of '-'    else:        i = s.find('-');     # Finding the real part    # of the complex number    real = s[:i]     # Finding the imaginary part    # of the complex number    imaginary = s[i + 1:l  - 1]     print("Real part:", real)    print("Imaginary part:", imaginary) # Driver codes = "3+4i"; findRealAndImag(s); # This code is contributed by Sanjit_Prasad
// C# program to find the real and// imaginary parts of a Complex Numberusing System; class GFG{    // Function to find real and imaginary    // parts of a complex number    static void findRealAndImag(String s)    {        // string length stored in variable l        int l = s.Length;               // variable for the index of the separator        int i;               // Storing the index of '+'        if (s.IndexOf('+') != -1) {            i = s.IndexOf('+');        }         // else storing the index of '-'        else {            i = s.IndexOf('-');        }                // Finding the real part        // of the complex number        String real = s.Substring(0, i);               // Finding the imaginary part        // of the complex number        String imaginary = s.Substring(i + 1, l - i - 2);               Console.WriteLine("Real part: " + real );        Console.WriteLine("Imaginary part: "+              imaginary);    }           // Driver code    public static void Main(String []args)    {        String s = "3+4i";               findRealAndImag(s);          }} // This code is contributed by 29AjayKumar
<script> // JavaScript program to find the real and// imaginary parts of a Complex Number     // Function to find real and imaginary    // parts of a complex number    function findRealAndImag(s)    {        // string length stored in variable l        let l = s.length - 1;              // variable for the index of the separator        let i;              // Storing the index of '+'        if (s.indexOf('+') != -1) {            i = s.indexOf('+');        }         // else storing the index of '-'        else {            i = s.indexOf('-');        }               // Finding the real part        // of the complex number        let real = s.substr(0, i);              // Finding the imaginary part        // of the complex number        let imaginary = s.substr(i + 1, l - 2 );              document.write("Real part: " + real +"<br/>" );        document.write("Imaginary part: "+              imaginary);    }      // Driver code     let s = "3+4i";          findRealAndImag(s); </script>
Real part: 3
Imaginary part: 4
 
Performance Analysis: 
Time Complexity: In the above approach, as we are doing a constant number of operations regardless of the length of string, the time complexity is O(1)
Auxiliary Space Complexity: In the above approach, we are not using any extra space apart from a few variables. So Auxiliary space complexity is O(1)
 
Sanjit_Prasad
ukasp
29AjayKumar
sanjoy_62
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo Operator (%) in C/C++ with Examples
Program to find GCD or HCF of two numbers
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java