Program to find sum and difference of complex numbers using classes in c++

This program takes two complex numbers as input and shows the sum and difference of the two complex numbers as output.

 

Code:

#include<iostream>
#include<math.h>
using namespace std;
class complex
{
    int a,b,c,d;
    public:
       void getdata();
        void display();
        void sum();
};


int main()
{
    complex c1;
   c1.getdata();
   c1.display();
   c1.sum();
}
void complex::getdata()
{
    cout<<"Enter the real and imaginary part of 1st complex number."<<endl;
    cin>>a>>b;
    cout<<"Enter the real and imaginary part of 2nd complex number."<<endl;
    cin>>c>>d;

}
void complex::display()
{

    cout<<"1st Complex Number is "<<a<<" + "<<b<<"i"<<endl;
    cout<<"2nd Complex Number is "<<c<<" + "<<d<<"i"<<endl;
}
void complex::sum()
{
    cout<<"The sum of the complex numbers is "<<a+c<<" + "<<b+d<<"i";
}

 

The code has been tested with codeblocks gcc compiler.

Leave a Reply

Your email address will not be published.