# Program to Add Two Complex Numbers

Given two complex numbers of the form and , the task is to add these two Complex Numbers.

Here the values of real and imaginary numbers is passed while calling the parametrised constructor and with the help of default(empty) constructor, the function addComp is called to get the addition of complex numbers.

Example:

```Input: a1 = 4, b1 = 8
a2 = 5, b2 = 7
Output: Sum = 9 + i15
Explanation:
(4 + i8) + (5 + i7)
= (4 + 5) + i(8 + 7)
= 9 + i15

Input: a1 = 9, b1 = 3
a2 = 6, b2 = 1
Output: Sum = 15 + i4
```

The following program is the illustration of the above example

## Java

 `// Java program to Add two complex numbers ` ` `  `import` `java.util.*; ` ` `  `// User Defined Complex class ` `class` `Complex { ` ` `  `    ``// Declaring variables ` `    ``int` `real, imaginary; ` ` `  `    ``// Empty Constructor ` `    ``Complex() ` `    ``{ ` `    ``} ` ` `  `    ``// Constructor to accept ` `    ``// real and imaginary part ` `    ``Complex(``int` `tempReal, ``int` `tempImaginary) ` `    ``{ ` `        ``real = tempReal; ` `        ``imaginary = tempImaginary; ` `    ``} ` ` `  `    ``// Defining addComp() method ` `    ``// for adding two complex number ` `    ``Complex addComp(Complex C1, Complex C2) ` `    ``{ ` `        ``// creating temporary variable ` `        ``Complex temp = ``new` `Complex(); ` ` `  `        ``// adding real part of complex numbers ` `        ``temp.real = C1.real + C2.real; ` ` `  `        ``// adding Imaginary part of complex numbers ` `        ``temp.imaginary = C1.imaginary + C2.imaginary; ` ` `  `        ``// returning the sum ` `        ``return` `temp; ` `    ``} ` `} ` ` `  `// Main Class ` `public` `class` `GFG { ` ` `  `    ``// Main function ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// First Complex number ` `        ``Complex C1 = ``new` `Complex(``3``, ``2``); ` ` `  `        ``// printing first complex number ` `        ``System.out.println(``"Complex number 1 : "` `                           ``+ C1.real + ``" + i"` `                           ``+ C1.imaginary); ` ` `  `        ``// Second Complex number ` `        ``Complex C2 = ``new` `Complex(``9``, ``5``); ` ` `  `        ``// printing second complex number ` `        ``System.out.println(``"Complex number 2 : "` `                           ``+ C2.real + ``" + i"` `                           ``+ C2.imaginary); ` ` `  `        ``// for Storing the sum ` `        ``Complex C3 = ``new` `Complex(); ` ` `  `        ``// calling addComp() method ` `        ``C3 = C3.addComp(C1, C2); ` ` `  `        ``// printing the sum ` `        ``System.out.println(``"Sum of complex number : "` `                           ``+ C3.real + ``" + i"` `                           ``+ C3.imaginary); ` `    ``} ` `} `

## C#

 `// C# program to Add two complex numbers ` `using` `System; ` ` `  `// User Defined Complex class ` `public` `class` `Complex  ` `{ ` ` `  `    ``// Declaring variables ` `    ``public` `int` `real, imaginary; ` ` `  `    ``// Empty Constructor ` `    ``public` `Complex() ` `    ``{ ` `    ``} ` ` `  `    ``// Constructor to accept ` `    ``// real and imaginary part ` `    ``public` `Complex(``int` `tempReal, ``int` `tempImaginary) ` `    ``{ ` `        ``real = tempReal; ` `        ``imaginary = tempImaginary; ` `    ``} ` ` `  `    ``// Defining addComp() method ` `    ``// for adding two complex number ` `    ``public` `Complex addComp(Complex C1, Complex C2) ` `    ``{ ` `        ``// creating temporary variable ` `        ``Complex temp = ``new` `Complex(); ` ` `  `        ``// adding real part of complex numbers ` `        ``temp.real = C1.real + C2.real; ` ` `  `        ``// adding Imaginary part of complex numbers ` `        ``temp.imaginary = C1.imaginary + C2.imaginary; ` ` `  `        ``// returning the sum ` `        ``return` `temp; ` `    ``} ` `} ` ` `  `// Main Class ` `public` `class` `GFG  ` `{ ` ` `  `    ``// Main function ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` ` `  `        ``// First Complex number ` `        ``Complex C1 = ``new` `Complex(3, 2); ` ` `  `        ``// printing first complex number ` `        ``Console.WriteLine(``"Complex number 1 : "` `                        ``+ C1.real + ``" + i"` `                        ``+ C1.imaginary); ` ` `  `        ``// Second Complex number ` `        ``Complex C2 = ``new` `Complex(9, 5); ` ` `  `        ``// printing second complex number ` `        ``Console.WriteLine(``"Complex number 2 : "` `                        ``+ C2.real + ``" + i"` `                        ``+ C2.imaginary); ` ` `  `        ``// for Storing the sum ` `        ``Complex C3 = ``new` `Complex(); ` ` `  `        ``// calling addComp() method ` `        ``C3 = C3.addComp(C1, C2); ` ` `  `        ``// printing the sum ` `        ``Console.WriteLine(``"Sum of complex number : "` `                        ``+ C3.real + ``" + i"` `                        ``+ C3.imaginary); ` `    ``} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

```Complex number 1 : 3 + i2
Complex number 2 : 9 + i5
Sum of complex number : 12 + i7
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : princi singh