# C++ Program to Add Two Complex Numbers Using Structure

Here is a C++ program to find sum of two complex numbers using structure. In this C++ program, we will add two complex numbers using a user defined structure. A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, which satisfies the equation i2 = -1.

In complex number a + bi, a is the real part and b is the imaginary part.

For Example :
```5 + 7i, -3 -2i, 2 - 6i
```

## How to Add Two Complex Numbers.

Let Sum(x + iy) is the sum of C1 and C2
Sum = C1 + C2
(x + iy) = (a + ib) + (c + id)
(x + iy) = (a + c) + i(b + d)
x = (a + c) and,
y = (b + d)

We will create a custom structure name "complex" which contains two member variables realPart and imaginaryPart.

```struct Complex {
int realPart;
int imaginaryPart;
};
```

We will use variables of structure Complex, to store complex numbers.

## C++ Program to Find Sum of Two Complex Numbers Using Structure

```#include <iostream>
using namespace std;

struct Complex {
int realPart;
int imaginaryPart;
};

int main() {
Complex c1, c2, sum;

cout << "Enter value of A and B  where A + iB iscomplex number\n";
cin >> c1.realPart >> c1.imaginaryPart;

cout << "Enter value of A and B  where A + iB is complex number\n";
cin >> c2.realPart >> c2.imaginaryPart;

/* (A + Bi) + (C + Di) = (A+C) + (B+D)i */
sum.realPart = c1.realPart + c2.realPart;
sum.imaginaryPart = c1.imaginaryPart + c2.imaginaryPart;

if(sum.imaginaryPart >= 0 )
cout << sum.realPart << " + " <<
sum.imaginaryPart<<"i";
else
cout << sum.realPart << " - " <<
sum.imaginaryPart<<"i";

return 0;
}
```
Output
```Enter value of A and B  where A + iB is complex number
2 5
Enter value of A and B  where A + iB is complex number
7 4
9 + 9i
```

In this program, we take two complex numbers as input from user in the form of A + iB and store in structure variable c1 and c2. We will add real parts of input complex numbers to get the real part of sum complex and add imaginary part of input complex numbers to get imaginary part of sum complex number.

Recommended Posts
 C++ Program to Store Information of a Book in a Structure C++ Program to Calculate Difference Between Two Time Periods C++ Program to Multiply Two Numbers Using Addition C++ Program to Store Information of an Employee in Structure C++ Program to Add Two Distances in Inch and Feet C++ Program to Find Area and Perimeter of Parallelogram C++ Program to Find Area and Circumference of a Circle C++ Program to Print Pascal Triangle C++ Program to Print Floyd Triangle C++ Program to Add Two Matrix C++ Program to Multiply Two Matrices All C++ Programs