Multiply N complex numbers given as strings

Given N Complex Numbers in the form of Strings, the task is to print the multiplication of these N complex numbers.


Input: N = 3, V = { 3 + 1i, 2 + 1i, 5 + -7i }
Output: 10+-60i
Firstly, we will multiply (3+1i) and (2+1i) to yield 5+5i. In the next step, we will multiply 5+5i and -5+-7i to yield the final result 10+-60i.

Input: N = 3, V = { “7+4i”, “-12+1i”, “-16+-7i”, “12+18i” }
Output: -9444+35442i


  • Firstly, start iterating from the beginning and take the first 2 Strings and erase both of them.
  • Next, convert the string into a number with appropriate signs. Store the Real Part and the Imaginary Part of the String in separate variables. Take a as the real part of the first string while b as the imaginary part of the first string. Take c as the real part of the second string while d as the imaginary part of the second string.
  • Next we will calculate the resultant values for the real by calculating the product of a and c and subtracting it with the product of b and subtracting with the product of b and d. For the Imaginary Part, we will calculate the sum of the product of a and d, along with the product of the b and c.
  • We will then generate a temporary string to take the sum of real and imaginary values that has been calculated earlier.
  • We will push the resultant string into the vector. Repeat the above steps until there is only one remaining element in the vector.
  • Return the last remaining element in the vector, which is the desired answer.

Below is the implementation of our above approach:






// C++ Program to multiply
// N complex Numbers
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// Function which returns the
// string in digit format
vector<long long int> findnum(string s1)
    vector<long long int> v;
    // a : real
    // b : imaginary
    int a = 0, b = 0;
    int sa = 0, sb = 0, i = 0;
    // sa : sign of a
    // sb : sign of b
    if (s1[0] == '-') {
        sa = 1;
        i = 1;
    // Extract the real number
    while (isdigit(s1[i])) {
        a = a * 10 + (int(s1[i]) - 48);
    if (s1[i] == '+') {
        sb = 0;
        i += 1;
    if (s1[i] == '-') {
        sb = 1;
        i += 1;
    // Extract the imaginary part
    while (i < s1.length() && isdigit(s1[i])) {
        b = b * 10 + (int(s1[i]) - 48);
    if (sa)
        a *= -1;
    if (sb)
        b *= -1;
    return v;
string complexNumberMultiply(vector<string> v)
    // if size==1 means we reached at result
    while (v.size() != 1) {
        // Extract the first two elements
        vector<ll> v1 = findnum(v[0]);
        vector<ll> v2 = findnum(v[1]);
        // Remove them
        // Calculate and store the real part
        ll r = (v1[0] * v2[0] - v1[1] * v2[1]);
        // Calculate and store the imaginary part
        ll img = v1[0] * v2[1] + v1[1] * v2[0];
        string res = "";
        // Append the real part
        res += to_string(r);
        res += '+';
        // Append the imaginary part
        res += to_string(img) + 'i';
        // Insert into vector
        v.insert(v.begin(), res);
    return v[0];
// Driver Function
int main()
    int n = 3;
    vector<string> v = { "3+1i",
                         "2+1i", "-5+-7i" };
    cout << complexNumberMultiply(v) << "\n";
    return 0;




Time Complexity: O(N)
Auxiliary Space: O(N)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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 or mail your article to 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 :

Be the First to upvote.

Please write to us at to report any issue with the above content.