Multiplication of Complex Numbers in C++
Multiplication of complex numbers in c++. This page provides c++ program for multiplication of two complex number.
A complex number is a type of number that contains real and imaginary numbers.
Every complex number can be represented in the form a + bi, where a and b are real numbers and i is imaginary number.
Multiplication of complex numbers in cpp
Here is the source code of C++ program to multiply two complex numbers.
/* Aim: Write a C++ program to multiply two complex numbers
#include<iostream>
using namespace std;
class complex
{
float a,b;
public:
complex()
{
a=b=1;
}
complex(float x, float y)
{
a=x;b=y;
}
complex operator * (complex c2)
{
complex tmp;
tmp.a=a*c2.a-b*c2.b;
tmp.b=a*c2.b+c2.a*b;
return tmp;
}
void display()
{
if(b>=0)
cout<<" Complex number : "<<a<<"+"<<b<<"i"<<endl;
else
cout<<" Complex number : "<<a<<b<<"i"<<endl;
}
void display(char op)
{
cout<<a<<"i"<<"/"<<b<<endl;
}
};
int main()
{
complex c1(7,-3),c2,c3;
c1.display();
c2.display();
cout<<" Multiplication is ";
c3=c1*c2;
c3.display();
return 0;
}
Output of above c++ program
Complex number : 7-3i Complex number : 1+1i Multiplication is Complex number : 10+4i
Related articles to multiplication of complex numbers in c++
You should check out this program on Addition of two complex numbers in cpp because it contains general explanation of how the program works.
Another similar program with different operator is division of complex numbers in c++.
Comments
Post a Comment