Find the real and imaginary part of a Complex number
Last Updated :
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: 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
- 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++
#include <bits/stdc++.h>
using namespace std;
void findRealAndImag(string s)
{
int l = s.length();
int i;
if (s.find( '+' ) < l) {
i = s.find( '+' );
}
else {
i = s.find( '-' );
}
string real = s.substr(0, i);
string imaginary = s.substr(i + 1, l - i - 2);
cout << "Real part: " << real << "\n" ;
cout << "Imaginary part: "
<< imaginary << "\n" ;
}
int main()
{
string s = "3+4i" ;
findRealAndImag(s);
return 0;
}
|
Java
class GFG
{
static void findRealAndImag(String s)
{
int l = s.length();
int i;
if (s.indexOf( '+' ) != - 1 ) {
i = s.indexOf( '+' );
}
else {
i = s.indexOf( '-' );
}
String real = s.substring( 0 , i);
String imaginary = s.substring(i + 1 , l - 1 );
System.out.println( "Real part: " + real );
System.out.println( "Imaginary part: " +
imaginary);
}
public static void main(String []args)
{
String s = "3+4i" ;
findRealAndImag(s);
}
}
|
Python3
def findRealAndImag(s) :
l = len (s)
i = 0
if (s.find( '+' ) ! = - 1 ):
i = s.find( '+' )
else :
i = s.find( '-' );
real = s[:i]
imaginary = s[i + 1 :l - 1 ]
print ( "Real part:" , real)
print ( "Imaginary part:" , imaginary)
s = "3+4i" ;
findRealAndImag(s);
|
C#
using System;
class GFG
{
static void findRealAndImag(String s)
{
int l = s.Length;
int i;
if (s.IndexOf( '+' ) != -1) {
i = s.IndexOf( '+' );
}
else {
i = s.IndexOf( '-' );
}
String real = s.Substring(0, i);
String imaginary = s.Substring(i + 1, l - i - 2);
Console.WriteLine( "Real part: " + real );
Console.WriteLine( "Imaginary part: " +
imaginary);
}
public static void Main(String []args)
{
String s = "3+4i" ;
findRealAndImag(s);
}
}
|
Javascript
<script>
function findRealAndImag(s)
{
let l = s.length - 1;
let i;
if (s.indexOf( '+' ) != -1) {
i = s.indexOf( '+' );
}
else {
i = s.indexOf( '-' );
}
let real = s.substr(0, i);
let imaginary = s.substr(i + 1, l - 2 );
document.write( "Real part: " + real + "<br/>" );
document.write( "Imaginary part: " +
imaginary);
}
let s = "3+4i" ;
findRealAndImag(s);
</script>
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...