Wednesday, October 17, 2012

Program to perform the addition of complex numbers



#include<iostream.h>
#include<conio.h>
class comp
{ private:
int r,i;
public:
void get();
void disp(comp,comp);
comp();
};
comp::comp()
{
r=0;
i=0;
}
void comp::get()

{
cout<<"\n\nEnter a complex number :-";
cout<<"\n\n\nEnter real part :\t";
cin>>r;
cout<<"\n\n\nEnter imaginary part:\t";
cin>>i;
cout<<"\n\n\nComlex number you entered :\t"<<r<<"+"<<i<<"i";
}
void comp::disp(comp a1,comp a2)
{
comp c;
c.r=a1.r+a2.r;

c.i=a1.i+a2.i;
cout<<"\n\n\n---------------------------------\n\n\nSum of the given complex numbers ="<<c.r<<"+"<<c.i<<"i";
}
void main()
{ clrscr();
comp a1,a2;
a1.get();
a2.get();
a1.disp(a1,a2);
getch();
}


Sample input and output

Enter a complex number
Enter real part:2
Enter imaginary part:3
Enter a complex number
Enter real part:1
Enter imaginary part:3
Sum of the given complex numbers =3+6i



2 comments: