Complex Number Multiplication in C++
In this tutorial, we will learn complex number multiplication in C++.
Complex Numbers
Complex numbers are a combination of a (real part) and b ( imaginary part )
written in the form a + bi
a, b are real numbers.
i2=-1, as no real number satisfies this equation so i is called an IMAGINARY number
Complex Numbers Multiplication:
Any two complex numbers can be multiplied to yield another complex number.
Let z1=a+bi and z2=c+di be two complex numbers where
On multiplying z1 and z2:
z1.z2=(a+bi)(c+di)
=ac+adi+bci+bdi2
=ac+(ad+bc)i-bd (as i2=-1)
=( ac – bd ) + ( ad + bc ) i //which is also a complex number
Complex Number Multiplication Properties:
Commutative Property
z1 = a+ ib
z2 = c + id
z 1 . z 2 = z1 . z2
Associative Property
z1 = a+ ib
z2 = c + id
z3 = e + if
(z1 . z2 ). z3 = (ace -bde – adf -bcf) + i(ade + bce + acf -bdf) = z 1 .(z 2 . z 3 )
Example
Complex Number Multiplication C++ Code
The class complex is created, each object of the class has two attributes- real and imag which are used for storing the real part and imaginary part of the complex number.
Each object of the class also has a function- called display( ), used to print the complex number in the form (real part)+ i(complex part).
#include<bits/stdc++.h>
using namespace std;
//C++ Program for multiplication of two complex nos
typedef struct Complex{
float real;
float imag;
void display();
}Complex;
void Complex::display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
Complex Multiply(Complex , Complex);
int main(){
Complex z1 , z2 , ans;
cout<<"Enter the real and imaginary parts of the first complex number:";
cin>>z1.real>>z1.imag;
cout<<"z1 = ";
z1.display();
//z1=a+bi
cout<<"Enter the real and imaginary parts of the second complex number:";
cin>>z2.real>>z2.imag;
cout<<"z2 = ";
z2.display();
//z2=c+di
//ans=(ac-bd)+i(ad+bc)
ans.real=(z1.real*z2.real)-(z1.imag*z2.imag);
ans.imag=(z1.real*z2.imag)+(z1.imag*z2.real);
cout<<"The result after the multiplication of z1 and z2 is: "<<endl;
cout<<"z1.z2 = ";
ans.display();
return 0;
}
Output
Enter the real and imaginary parts of the first complex number: 4 6
z1 = 4+6i
Enter the real and imaginary parts of the second complex number: 3 8
z2 = 3+8i
The result after the multiplication of z1 and z2 is:
z1.z2 = -36+50i
Leave a Reply