# C++ how to add,subtract,multiply,divide two complex numbers?

In this post we will discuss two programs to add,subtract,multiply and divide two complex numbers with C++.In the first program we will not use any header or library to perform the operation.The second program will make use of the C++ complex header <complex> to perform the operation.The two programs are given below.

In this method our program will consists of a structure that will hold the real and imaginary number of the two complex numbers.We will also define different functions for addition ,subtraction ,multiplication and division of the complex number.In this way we can organize our program better for maintainability and better understanding.The structure is given below.

```struct ComplexNumber
{
double real1 , real2 , //holds the real number
img1 , img2 ; //holds the imaginary number
};
```

The double type is prefferrable because it is big enough to hold any large number.

In addition function we will add the real part separately and the imaginary part separately.The complex number resulting from their individual separate sum is the resultant complex number.The function is given below.

```void addition( ComplexNumber &cn )
{
cout<<“\nThe sum of the two complex numbers: \n”
<< ” (” << cn.real1 << “+” << cn.img1 << “i) + (” << cn.real2 << “+” << cn.img2 << “i) ”

<< “= ” << cn.real1+cn.real2 //addition of real part
<< “+” << cn.img1 + cn.img2 << “i” ; //addition of imaginary part
}
```

#### The subtraction function

To get the resultant subtracted complex number the real part of the second complex number is subtracted from the real part of the first complex number and to get the imaginary part we subtract the imaginary part of the second complex number from the imaginary part of the first complex number.The function definition of subtraction is shown below.

```void subtraction( ComplexNumber &cn )
{
cout<<“\n\nThe subtraction of the two complex numbers: \n”
<< ” (” << cn.real1 << “+” << cn.img1 << “i) – (” << cn.real2 << “+” << cn.img2 << “i) ” ;

if( cn.img1 – cn.img2 < 0 ) /*checks if the imaginary number of second complex number is -ve,if so put it in bracket */
{
cout<<“= ” << cn.real1-cn.real2 << “+(” << cn.img1 – cn.img2 << “)i” ;
}
else
cout << “= ” << cn.real1-cn.real2 << “+” << cn.img1 – cn.img2 << “i” ;
}
```

The if() statement is used to check if the imaginary part is a negative value and if so we will put the value under the bracket .The bracket will distinguished the negative sign of the imaginary value from the normal addition sign of the complex number.

#### The multiplication function

To get the product of the two complex number we simply evaluate the expression (ac-bd)+(bc+ad)i considering that a+ib and c+id are the two complex numbers.The function definition of the multiplication function is shown below.

```void multiplication( ComplexNumber &cn )
{
cout<<“\n\nThe product of the two complex numbers: \n”
<< ” (” << cn.real1 << “+” << cn.img1 << “i) * (” << cn.real2 << “+” << cn.img2 << “i) ”

<< “= ” << (cn.real1*cn.real2)- (cn.img1*cn.img2) << “+” << (cn.img1*cn.real2) + (cn.img2*cn.real1) << “i” ;
}
```

#### The division function

To divide a+bi by c+id we will perform the operation (ac+bd)/(c2 + d2) + (bc-ad)/(c2 + d2)i.

```void division( ComplexNumber &cn )
{
cout<<“\n\nThe division of the two complex numbers: \n”
<< ” (” << cn.real1 << “+” << cn.img1 << “i) / (” << cn.real2 << “+” << cn.img2 << “i) ”

<< “= (” << (cn.real1*cn.real2)+(cn.img1*cn.img2) << “/” << pow(cn.real2,2) + pow(cn.img2,2)

<< “)+(” << (cn.img1*cn.real2) – (cn.img2*cn.real1) << “/” <<pow(cn.real2,2) + pow(cn.img2,2) << “)i” ;//pow() function is included in <cmath> header
}
```

The pow() function will calculate the square of real and imaginary value of the denominator complex number.

#### The main function

The main() function does nothing it simply asks the user to input the two complex numbers.The whole program is shown below.

```#include <iostream>
#include <cmath>

using name space std;

//The structure that holds the complex number
struct ComplexNumber
{
double real1 , real2 ,
img1 , img2 ;
};

{
cout<<“\nThe sum of the two complex numbers: \n”
<< ” (” << cn.real1 << “+” << cn.img1 << “i) + (” << cn.real2 << “+” << cn.img2 << “i) ”

<< “= ” << cn.real1+cn.real2 << “+” << cn.img1 + cn.img2 << “i” ;
}

//the subtraction function
void subtraction( ComplexNumber &cn )
{
cout<<“\n\nThe subtraction of the two complex numbers: \n”
<< ” (” << cn.real1 << “+” << cn.img1 << “i) – (” << cn.real2 << “+” << cn.img2 << “i) ” ;

if( cn.img1 – cn.img2 < 0 )
{
cout<<“= ” << cn.real1-cn.real2 << “+(” << cn.img1 – cn.img2 << “)i” ;
}
else
cout << “= ” << cn.real1-cn.real2 << “+” << cn.img1 – cn.img2 << “i” ;
}

//the multiplication
void multiplication( ComplexNumber &cn )
{
cout<<“\n\nThe product of the two complex numbers: \n”
<< ” (” << cn.real1 << “+” << cn.img1 << “i) * (” << cn.real2 << “+” << cn.img2 << “i) ”

<< “= ” << (cn.real1*cn.real2)- (cn.img1*cn.img2) << “+” << (cn.img1*cn.real2) + (cn.img2*cn.real1) << “i” ;
}

//the division function
void division( ComplexNumber &cn )
{
cout<<“\n\nThe division of the two complex numbers: \n”
<< ” (” << cn.real1 << “+” << cn.img1 << “i) / (” << cn.real2 << “+” << cn.img2 << “i) ”

<< “= (” << (cn.real1*cn.real2)+(cn.img1*cn.img2) << “/” << pow(cn.real2,2) + pow(cn.img2,2)

<< “)+(” << (cn.img1*cn.real2) – (cn.img2*cn.real1) << “/” << pow(cn.real2,2) + pow(cn.img2,2) << “)i” ;///sqrt() function included in <cmath> library
}

//the main() function
int main( )
{
ComplexNumber cn ;

cout<<“Enter the real and imaginary value of the first complex number\n” ;
cin >> cn.real1 >> cn.img1 ;
cin.ignore( ) ;

cout<<“\nEnter the real and imaginary value of the second complex number\n” ;
cin>> cn.real2 >> cn.img2 ;
cin.ignore( ) ;

subtraction(cn) ; //perform subtraction
multiplication(cn) ; //perform multiplication
division( cn ) ; //perform division

cin.get( ) ;
return 0 ;
}
```

The screen shot of the output with the two complex numbers as 3+i4 and 5+i8 is shown below.

If we use the header <complex> the addition,subtraction,multiplication and division of complex number becomes easy.We can declare two complex numbers of the type complex and treat the complex numbers like the normal number and perform the addition,subtraction,multiplication and division like we usually do.The program is given below.

```#include <iostream>
#include <complex>

using namespace std ;

void add(complex<double> &c1 , complex<double> &c2)
{
cout<<“The sum of “<< c1 <<” and “<< c2 << “complex numbers =” << c1+c2 ; //we directly add the two complex nubmers to obtain the sum
}

//the subtraction function
void subtract(complex<double> &c1 , complex<double> &c2)
{
cout<<“\n\nThe subtraction of “<< c1 <<” and “<< c2 << “complex numbers =” << c1-c2 ; //we directly subtract the two complex nubmers to obtain the subtraction
}

//the multiplication function
void multiply(complex<double> &c1 , complex<double> &c2)
{
cout<<“\n\nThe multiplication of “<< c1 <<” and “<< c2 << “complex numbers =” << c1*c2 ; //we directly multiply the two complex nubmers to obtain the product
}

//the division function
void divide(complex<double> &c1 , complex<double> &c2)
{
cout<<“\n\nThe division of “<< c1 <<” and “<< c2 << “complex numbers =” << c1/c2 ; //we directly divide the two complex nubmers to obtain the value
}

//the main fucntion
int main( )
{
complex<double> c1 , c2 ; //a complex number type declaration

double c1R , c1I , c2R , c2I ;

cout<<“Enter the real and imaginary value of the first complex number \n”;
cin>> c1R >> c1I ;

cin.ignore( );

c1.real( c1R ); //Assign the real value of c1 complex as c1R
c1.imag( c1I ); //Assign the imaginary value of c1 complex as c1I

cout<<“Enter the real and imaginary value of the second complex nubmer \n”;
cin>> c2R >> c2I ;

cin.ignore( );

c2.real( c2R ); //Assign the real value of c2 complex as c2R
c2.imag( c2I ); //Assign the imaginary value of c2 complex as c2I

//Subtracting two complex numbers
subtract( c1 , c2 );

//Multiplying two complex numbers
multiply( c1 , c2 );

//Dividing two complex numbers
divide( c1 , c2 );

cin.get( ) ;
return 0 ;
}
```

The output is shown below,

Enter the real and imaginary value of the first complex nubmer
3 4
Enter the real and imaginary value of the second complex nubmer
5 8
The sum of (3,4) and (5,8)complex numbers =(8,12)

The subtraction of (3,4) and (5,8)complex numbers =(-2,-4)

The multiplication of (3,4) and (5,8)complex numbers =(-17,44)

The division of (3,4) and (5,8)complex numbers =(0.52809,-0.0449438)