Java Program to Add Two Complex Numbers

In this post, you will learn to add two complex numbers in the Java Programming language. You will learn to code the java program to add two complex numbers.

Before getting to the code let’s understand “What is a complex number?”

What is a complex number ?

Complex numbers are numbers that consist of two parts – a real number and an imaginary number. Complex numbers are generally represented in this form: a + i b, where i stands for “iota”

Addition of complex number

Now, let’s see the “How complex numbers are added?”
a + i c = 10 + 5i
b + i d = 20 + 6i
sum = (a+b) + (c+d) i = 30 + 11 i

Now, let’s get straight into the Java program to add two complex numbers.

Java program to add two complex numbers

```import java.util.*;

public class Main{

public static class ComplexNumber{
double real;
double imaginary;

ComplexNumber(){}
ComplexNumber(double real,double imaginary){
this.real = real;
this.imaginary = imaginary;
}

}

public static ComplexNumber sum(ComplexNumber a, ComplexNumber b){
ComplexNumber ans = new ComplexNumber();
ans.real = a.real + b.real;
ans.imaginary = a.imaginary + b.imaginary;
return ans;
}

public static void main(String[] args){
// without taking inputs
ComplexNumber c1 = new ComplexNumber(3,5);
ComplexNumber c2 = new ComplexNumber(4,6);

ComplexNumber ans = sum(c1,c2);
System.out.println("The addition of two complex numbers is : "+ans.real+" + "+ans.imaginary+"i");

// Now using users input
Scanner sc = new Scanner(System.in);
ComplexNumber num1 = new ComplexNumber();
ComplexNumber num2 = new ComplexNumber();
System.out.println("Enter first complex number: \nreal: ");
num1.real = sc.nextDouble();
System.out.println("\nimaginary: ");
num1.imaginary = sc.nextDouble();
System.out.println("Enter Second complex number: \nreal: ");
num2.real = sc.nextDouble();
System.out.println("\nimaginary: ");
num2.imaginary = sc.nextDouble();

ComplexNumber answer = sum(num1,num2);
System.out.println("The addition of two complex numbers is : "+answer.real+" + "+answer.imaginary+"i");

}

}

```

Output

``````The addition of two complex numbers is : 7.0 + 11.0i
Enter first complex number:
real: 13
imaginary:
2
Enter Second complex number:
real:
13
imaginary: 2
The addition of two complex numbers is : 26.0 + 4.0i

``````

How Does This Program Work

In the above program we have created a complex numbers class as we don’t have any pre defined datatype to declare a complex number.

``````public static class ComplexNumber{
double real;
double imaginary;

ComplexNumber(){}
ComplexNumber(double real,double imaginary){
this.real = real;
this.imaginary = imaginary;
}

}``````

We created a class ComplexNumber with 2 data member i.e. real and imaginary to store 2 parts of the complex numbers. Then we have created the constructor. We have also created a parametarized constructor to create the instance of the ComplexNumber Class with given values of the real and imaginary part respectively.

`````` public static ComplexNumber sum(ComplexNumber a, ComplexNumber b){
ComplexNumber ans = new ComplexNumber();
ans.real = a.real + b.real;
ans.imaginary = a.imaginary + b.imaginary;
return ans;
}``````

Then we have created a sum function which takes 2 complex numbers as input and returns the sum after performing the complex number addition i.e. (real1 + real2) + (img1+img2) i

``````ComplexNumber c1 = new ComplexNumber(3,5);
ComplexNumber c2 = new ComplexNumber(4,6);

ComplexNumber ans = sum(c1,c2);
System.out.println("The addition of two complex numbers is : "+ans.real+" + "+ans.imaginary+"i");
``````

Here, we have created 2 complex numbers by providing the value by own and then performing the complex addition.

``````Scanner sc = new Scanner(System.in);
ComplexNumber num1 = new ComplexNumber();
ComplexNumber num2 = new ComplexNumber();
System.out.println("Enter first complex number: \nreal: ");
num1.real = sc.nextDouble();
System.out.println("\nimaginary: ");
num1.imaginary = sc.nextDouble();
System.out.println("Enter Second complex number: \nreal: ");
num2.real = sc.nextDouble();
System.out.println("\nimaginary: ");
num2.imaginary = sc.nextDouble();
ComplexNumber answer = sum(num1,num2);
System.out.println("The addition of two complex numbers is : "+answer.real+" + "+answer.imaginary+"i");``````

Here, we have taken the value of the complex numbers from the user and then have performed the following additon and displayed the result ðŸ˜„

Conclusion

I hope after going through this post, you understand how to code Java program to add two complex numbers.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read: