Skip to content
Related Articles

Related Articles

Find the real and imaginary part of a Complex number
  • Difficulty Level : Easy
  • Last Updated : 05 May, 2020

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++




// 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;
}

Java




// 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

Python3




# 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

C#




// 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
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)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :