Related Articles

# Multiplication of two complex numbers given as strings

• Difficulty Level : Easy
• Last Updated : 14 Jun, 2021

Given two complex numbers in the form of strings. Our task is to print the multiplication of these two complex numbers.

Examples:

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.

Input : str1 = "1+1i"
str2 = "1+1i"
Output : "0+2i"
Here, (1 + i) * (1 + i) =
1 + i2 + 2 * i = 2i or "0+2i"

Input : str1 = "1+-1i"
str2 = "1+-1i"
Output : "0+-2i"
Here, (1 - i) * (1 - i) =
1 + i2 - 2 * i = -2i or "0+-2i"

Multiplication of two complex numbers can be done as: We simply split up the real and the imaginary parts of the given complex strings based on the ‘+’ and the ‘i’ symbols. We store the real parts of the two strings a and b as x and y respectively and the imaginary parts as x and y respectively. Then, we multiply the real and the imaginary parts as required after converting the extracted parts into integers. Then, we again form the return string in the required format and return the result.

## C++

 // C++ Implementation of the above approach#include using namespace std;string complexNumberMultiply(string a, string b){    int i;    string x1;    int temp = 1;     // Traverse both strings, and    // check for negative numbers    for (i = 0; i < a.length(); i++)    {        if (a[i] == '+')            break;        if (a[i] == '-')        {            temp = -1;            continue;        }        x1.push_back(a[i]);    }         // String to int    int t1 = stoi(x1) * temp;    x1.clear();    temp = 1;    for (; i < a.length() - 1; i++)    {        if (a[i] == '-')        {            temp = -1;            continue;        }        x1.push_back(a[i]);    }    int t2 = stoi(x1) * temp;    x1.clear();    temp = 1;    for (i = 0; i < b.length(); i++)    {        if (b[i] == '+')            break;        if (b[i] == '-')        {            temp = -1;            continue;        }        x1.push_back(b[i]);    }    int t3 = stoi(x1) * temp;    x1.clear();    temp = 1;    for (; i < b.length() - 1; i++)    {        if (b[i] == '-')        {            temp = -1;            continue;        }        x1.push_back(b[i]);    }    int t4 = stoi(x1) * temp;        // Real Part    int ans = t1 * t3 - t2 * t4;    string s;    s += to_string(ans);    s += '+';         // Imaginary part    ans = t1 * t4 + t2 * t3;    s += to_string(ans);    s += 'i';     // Return the result    return s;}   // Driver Codeint main(){     string str1 = "1+1i";    string str2 = "1+1i";    cout << complexNumberMultiply(str1, str2);     return 0;     // Contributed By Bhavneet Singh}

## Java

 // Java program to multiply two complex numbers// given as strings.import java.util.*;import java.lang.*; public class GfG{    public static String complexNumberMultiply(String a, String b) {         // Spiting the real and imaginary parts        // of the given complex strings based on '+'        // and 'i' symbols.        String x[] = a.split("\\+|i");        String y[] = b.split("\\+|i");                 // Storing the real part of complex string a        int a_real = Integer.parseInt(x);                 // Storing the imaginary part of complex string a        int a_img = Integer.parseInt(x);                 // Storing the real part of complex string b        int b_real = Integer.parseInt(y);                 // Storing the imaginary part of complex string b        int b_img = Integer.parseInt(y);                 // Returns the product.        return (a_real * b_real - a_img * b_img) + "+" +              (a_real * b_img + a_img * b_real) + "i";    }         // Driver function    public static void main(String argc[]){        String str1 = "1+1i";        String str2 = "1+1i";        System.out.println(complexNumberMultiply(str1, str2));    }}

## Python3

 # Python 3 program to multiply two complex numbers# given as strings.def complexNumberMultiply(a, b):         # Spiting the real and imaginary parts    # of the given complex strings based on '+'    # and 'i' symbols.    x = a.split('+')    x = x[:-1] # for removing 'i'         y = b.split("+")    y = y[:-1] # for removing 'i'         # Storing the real part of complex string a    a_real = int(x)             # Storing the imaginary part of complex string a    a_img = int(x)                  # Storing the real part of complex string b    b_real = int(y)                  # Storing the imaginary part of complex string b    b_img = int(y)    return str(a_real * b_real - a_img * b_img) \        + "+" + str(a_real * b_img + a_img * b_real) + "i";      # Driver function str1 = "1 + 1i"str2 = "1 + 1i"print(complexNumberMultiply(str1, str2)) # This code is contributed by ANKITKUMAR34

## C#

 // C# program to multiply two complex// numbers given as strings.using System;using System.Text.RegularExpressions; class GfG{     public static String complexNumberMultiply(String a,                                           String b){         // Spiting the real and imaginary parts    // of the given complex strings based on '+'    // and 'i' symbols.    String []x = Regex.Split(a, @"\+|i");    String []y = Regex.Split(b, @"\+|i");     // Storing the real part of complex string a    int a_real = Int32.Parse(x);         // Storing the imaginary part of complex string a    int a_img = Int32.Parse(x);         // Storing the real part of complex string b    int b_real = Int32.Parse(y);         // Storing the imaginary part of complex string b    int b_img = Int32.Parse(y);         // Returns the product.    return(a_real * b_real - a_img * b_img) + "+" +          (a_real * b_img + a_img * b_real) + "i";} // Driver codepublic static void Main(String []argc){    String str1 = "1+1i";    String str2 = "1+1i";         Console.WriteLine(complexNumberMultiply(str1, str2));}} // This code is contributed by shikhasingrajput

## PHP

 

## Javascript

 

Output:

0+2i

My Personal Notes arrow_drop_up