Java Program to Add Complex Numbers

library function fabs in math class

What is  Complex Number ?

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, defined as i = sqrt(-1). The number a is called the real part of the complex number, and b is called the imaginary part.

For example, the complex number 3 + 4i has a real part of 3 and an imaginary part of 4.

Complex numbers :

Complex numbers are useful in mathematics and physics because they can represent quantities that have both magnitude and direction, such as angular velocity and electric current. They are also useful in engineering and other fields because they can be used to represent sinusoidal functions, which are periodic functions that are commonly found in signals and systems.

1. In Java, you can use the java.lang.Complex class to represent and manipulate complex numbers. For example:

import java.lang.Complex;

Complex z1 = new Complex(3, 4); // 3 + 4i
Complex z2 = new Complex(1, 2); // 1 + 2i

// add two complex numbers
Complex z3 = z1.add(z2); // (3 + 4i) + (1 + 2i) = 4 + 6i

2. You can also use the java.math.BigDecimal class to represent complex numbers with arbitrary precision. For example:

import java.math.BigDecimal;

BigDecimal re1 = new BigDecimal("3");
BigDecimal im1 = new BigDecimal("4");
BigDecimal re2 = new BigDecimal("1");
BigDecimal im2 = new BigDecimal("2");

// add two complex numbers
BigDecimal re3 = re1.add(re2);
BigDecimal im3 = im1.add(im2);

Example :

Run

public class Main {
  public static void main(String[] args) {
    class Complex {
      private double re;
      private double im;

      public Complex(double re, double im) {
        this.re = re;
        this.im = im;
      }

      public Complex add(Complex other) {
        return new Complex(re + other.re, im + other.im);
      }

      public double getReal() {
        return re;
      }

      public double getImaginary() {
        return im;
      }

      @Override
      public String toString() {
        return re + " + " + im + "i";
      }
    }

    Complex z1 = new Complex(3, 4); // 3 + 4i
    Complex z2 = new Complex(1, 2); // 1 + 2i

    Complex z3 = z1.add(z2);
    System.out.println(z3); // prints "4.0 + 6.0i"
  }
}

Output :

4.0 + 6.0i

Example :

Run
import java.math.BigDecimal;

public class Main {
  public static void main(String[] args) {
    BigDecimal re1 = new BigDecimal("3");
    BigDecimal im1 = new BigDecimal("4");
    BigDecimal re2 = new BigDecimal("1");
    BigDecimal im2 = new BigDecimal("2");

    // add two complex numbers
    BigDecimal re3 = re1.add(re2);
    BigDecimal im3 = im1.add(im2);

    System.out.println(re3 + " + " + im3 + "i"); // prints "4 + 6i"
  }
}

Output :

4 + 6i

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription