C++ Program to add two complex numbers
Program
#include <iostream>
using namespace std;
class complex
{
public:
int real, img;
};
int main()
{
complex x, y, z;
cout << "Enter values for x1 + iy1 complex number:" << endl;
cout << "Enter value for x1:\t";
cin >> x.real;
cout << "Enter value for y1:\t";
cin >> x.img;
cout << "Enter values for x2 + iy2 complex number:" << endl;
cout << "Enter value for x2:\t";
cin >> y.real;
cout << "Enter value for y2:\t";
cin >> y.img;
z.real = x.real + y.real;
z.img = x.img + y.img;
cout << "Sum of two complex numbers is:" << endl;
if (z.img >= 0)
cout << z.real << " + " << z.img << "i";
else
cout << z.real << " " << z.img << "i";
cout << endl;
return 0;
}
Output
Enter values for x1 + iy1 complex number:
Enter value for x1: 5
Enter value for y1: 12
Enter values for x2 + iy2 complex number:
Enter value for x2: 7
Enter value for y2: 4
Sum of two complex numbers is:
12 + 16i