Find the conjugate of a Complex number

Given a complex number str in the form of a string, the task is to determine the conjugate of this complex number.

Examples:

Input: str = "3 - 4i"
Output: 3 + 4i

Input: str = "6 - 5i"
Output: 6 + 5i

Approach: A complex number is said to be a conjugate of another complex number if only the sign of the imaginary part of the two numbers is different.

If complex number = x + iy

Conjugate of this complex number = x - iy

Below is the implementation of the above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to Find the
// conjugate of a complex number
#include <bits/stdc++.h>
using namespace std;
  
// Function to find conjugate
// of a complex number
void solve(string s)
{
    string z = s;
    int l = s.length();
    int i;
  
    if (s.find('+') < l) {
  
        // store index of '+'
        i = s.find('+');
  
        replace(s.begin(),
                s.end(),
                '+', '-');
    }
    else {
  
        // store index of '-'
        i = s.find('-');
  
        replace(s.begin(),
                s.end(),
                '-', '+');
    }
  
    // print the result
    cout << "Conjugate of "
         << z << " = "
         << s << endl;
}
  
// Driver code
int main()
{
  
    // initialise the complex number
    string s = "3-4i";
  
    solve(s);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to Find the
// conjugate of a complex number
  
class GFG
{
    // Function to find conjugate
    // of a complex number
    static void solve(String s)
    {
        String z = s;
        int l = s.length();
        int i;
        String str;
  
        if (s.indexOf('+') != -1) {
       
            // store index of '+'
            i = s.indexOf('+');
       
            str = s.replace('+', '-');
        }
        else {
       
            // store index of '-'
            i = s.indexOf('-');
       
            str = s.replace('-', '+');
        }
       
        // print the result
        System.out.println("Conjugate of "
             + z + " = "
             + str);
    }
       
    // Driver code
    public static void main(String []args)
    {
       
        // initialise the complex number
        String s = "3-4i";
       
        solve(s);
    }
}
  
// This code is contributed by chitranayal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to Find the 
# conjugate of a complex number 
  
# Function to find conjugate 
# of a complex number 
def solve(s):
    z = s
    l = len(s) 
    i = 0
    if (s.find('+') != -1):
    
        # store index of '+' 
        i = s.find('+')
    
        s = s.replace('+', '-')
    else:
        # store index of '-' 
        i = s.find('-')
  
        s = s.replace('-', '+',1)
    
    # print the result 
    print("Conjugate of ",z," = ",s)
  
  
# Driver code 
  
# initialise the complex number 
s = "3-4i"
solve(s)
  
# This code is contributed by Sanjit_Prasad

chevron_right


Output:

Conjugate of 3-4i = 3+4i

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.



Improved By : Sanjit_Prasad, chitranayal