How to Add Two Complex Numbers in Java

In this tutorial, we are going to see how to calculate the sum of two complex numbers that will be requested from the user, then display them. The user must enter the real and imaginary parts of the two complex numbers. For example, if a user enters two complex numbers (1 + 2i) and (4 + 6i), then the output of the program will be (5 + 8i).

Java Program to Add Two Complex Numbers:
public class Complex {

    double r;
    double i;

    public Complex(double r, double i) {
        this.r = r;
        this.i = i;

    public static Complex sumCplex(Complex c1, Complex c2)
        Complex c3 = new Complex(0.0, 0.0);

        c3.r = c1.r + c2.r;
        c3.i = c1.i + c2.i;

        return c3;
    public static void main(String[] args) {
        Complex c1 = new Complex(1.5, 3.1);
        Complex c2 = new Complex(2.9, 6.1);
        Complex c3;

        c3 = sumCplex(c1, c2);

        System.out.printf("The sum of the two numbers is : %.1f + %.1fi", c3.r, c3.i);


The sum of the two numbers is : 4.4 + 9.2i
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *