#### C++ Programming

## C++ program to add two complex numbers

**Add two complex numbers** : Complex number has two part. One is real part and other is imaginary part. For this we have to add these two part separately and print the result after addition.

For example, **a + bi** is a complex number where bi is the imaginary part. In this add complex numbers program we will take two integer a and b from the user where the complex number will be a + bi. Then we will add these two number using a class and print the output.

Consider a user gives the value of **a** and **b** for first complex number is 7 and 4. So, the complex number will be,

**7 + 4i**

Again he gives the value of **a** and **b** for the second complex number 2 and 8. Then the second complex number is,

**2 + 8i**

We have to print the result after add the two complex numbers as follows;

**9 + 12i**

## Program to add two complex numbers

The source code to add two complex numbers is given bellow. You can add more than two numbers by the same logic.

```
// c++ program to add two complex numbers
#include <iostream>
using namespace std;
class complex_num{
public :
int real_part, img_part;
};
int main(){
complex_num a, b, result;
cout << "For first complex number,\n";
cout << "Enter the value of a and b where complex number is a + ib : \n";
cin >> a.real_part >> a.img_part;
cout << "\nFor second complex number,\n";
cout << "Enter the value of a and b where complex number is a + ib : \n";
cin >> b.real_part >> b.img_part;
result.real_part = a.real_part + b.real_part;
result.img_part = a.img_part + b.img_part;
if (result.img_part >= 0){
cout << "\nSum is = " << result.real_part << " + " << result.img_part << "i\n";
}else{
cout << "\nSum is = " << result.real_part << " " << result.img_part << "i\n";
}
return 0;
}
```

### Output of add complex number program:

You can also subtract a complex number from another using the same rule. Try this addition program using more than two complex numbers now.

**Previous page**:Â **Multiply and divide**

**Next page**:Â **Prime numbers**