C++ Program to Multiply two Complex Numbers

In this tutorial, we will write a C++ program to perform a multiplication on two complex numbers. Before that, you may go through the following topic in java.

C++ Program to Multiply two Complex Numbers using Struct

#include <stdio.h>

//structure
typedef struct COMPLEX
{
   int a;
   int b;
}

Complex;

//user-defined function to calculate
Complex multiplicationFunc(Complex x, Complex y)
{
   Complex z;
   z.a = x.a *y.a - x.b *y.b;
   z.b = x.a *y.b + x.b *y.a;
   return z;
}

//Main function
int main()
{
   int a1, b1, a2, b2;
   Complex x, y, z;

   printf("Enter first complex number (a+bi) : ");
   scanf("%d+%di", &a1, &b1);
   printf("Enter second complex number (a+bi) : ");
   scanf("%d+%di", &a2, &b2);

   x.a = a1;
   x.b = b1;
   y.a = a2;
   y.b = b2;

   //calling multiplication function
   z = multiplicationFunc(x, y);

   //Printing the result
   printf("\nFinal multiplication result: %d+%di", z.a, z.b);
   return 0;
}

Output:

Enter first complex number (a+bi) : 2+3i
Enter second complex number (a+bi) : 2+3i

Final multiplication result: -5+12i