Find the real and imaginary part of a Complex number

Given a complex number Z, the task is to determine the real and imaginary part of this complex number.

Examples:

Input: z = 3 + 4i
Output: Real part: 3, Imaginary part: 4

Input: 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



  1. Find out the index of + or operator in the string
  2. Real part will be a substring starting from index 0 to a length (index of operator – 1)
  3. Imaginary part will be a substring starting from index (index of operator + 1) to (length of string – index of operator – 2)

Implementation:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 number
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.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 code
int main()
{
    string s = "3+4i";
  
    findRealAndImag(s);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the real and
// imaginary parts of a Complex Number 
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 - 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the real and 
# imaginary parts of a Complex Number 
  
# Function to find real and imaginary 
# parts of a complex number 
def 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 code 
s = "3+4i"
  
findRealAndImag(s); 
  
# This code is contributed by Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the real and
// imaginary parts of a Complex Number 
using 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

chevron_right


Output:

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)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.