C++ program to add , subtract , multiply and divide two complex numbers


In this post we will discuss two programs to add,subtract,multiply and divide two complex numbers.The first program will use the C++ complex header <complex> and the second program doesn’t use the header.The two programs are given below.

With <complex> header

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.

The only problem with this program is you need to understand what is template first because the complex class is a template class.If you don’t know what is template you can use the program given in the next section which does not use the <complex> header.

Link: C++ complex header

Code example

#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
}

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
}

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
}

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
}

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

//adding two complex number
add( c1 , c2 );

//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)

Link :C++ <complex> imag function

Link :C++ <complex> real function





Without the <complex> header

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.In this way we can organize our program better for maintainability and better understanding.The structure that would hold the two complex number is shown below.

struct ComplexNumber
{
double real1 , real2 ,
  img1 , img2 ;
};

The type of the real and imaginary value is double not float.Double should be preferred because it is big enough to hold any large number and also using double would make your program run faster over using float or long double type.So use double.

Link : Why double should be preferred?


Addition

Suppose if a+ib and c+id are the two complex numbers,to get their sum we will add the real part of the two complex numbers separately and the imaginary parts separately.The complex number resulting from their individual separate sum is the resultant complex number.The code of function performing the addition is shown 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 << “+” << cn.img1 + cn.img2 << “i” ;
}

Nothing extra ordinary is done here we simply add a+c and b+d to give (a+c) + (b+d)i which is the resultant complex number.


Subtraction

The second function is subtraction function.This function purpose is to subtract the two complex numbers.Here also let’s consider the two complex numbers as a+ib and c+id .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,that means (a-c) is performed and to get the imaginary part we perform (b-d).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 )
{
  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.


 


Multiplication

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” ;
}


Division

To divide a+bi by c+id we usually multiply c-id to a+ib and c+id.The expression resulting from the multiplication is (ac+bd)/(c2 + d2) + (bc-ad)/(c2 + d2)i .The division function definition is shown below.

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.This function is found in the <cmath> library so do not forget to include the header name.

Link C++ cmath pow , powf , powl





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;

struct ComplexNumber
{
double real1 , real2 ,
  img1 , img2 ;
};

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 << “+” << cn.img1 + cn.img2 << “i” ;
}

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” ;
}

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” ;
}

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
}

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( ) ;

addition(cn) ; ///perform addition

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.

C++ program to add,subtract,multiply and divide two complex numbers


Related Link

-> A C++ program to find month,date and day after 50 days from the current date.

-> C++ program to compute the compound interest of any number of clients.