How to write a java program to add two complex numbers using function??

    Complex numbers are numbers which consists of two parts. First part is real number and second part is imaginary. The general format is a+ib where a, b is rational numbers.

How to write a java program to add two complex numbers using function :

  • ·       First, create a class “complexAdd” and add two member variables in double datatype.
  • ·       Let name this variables as realNo and imgNo(Imaginary number).
  • ·       Initialize the complex numbers using constructor.
  • ·       Create two functions sum() and display().
  • ·       In sum() function, add the two complex variable’s real parts and imaginary parts separately.
  • ·       Define the display() function to print the sum of the two complex numbers.

class complexAdd {

    double realNo;

    double imgNo;

    // Create the constructor to initialize the values

    public complexAdd(double realNo, double imgNo) {

        this.realNo = realNo;

        this.imgNo = imgNo;

    }

     // Definition of addition of two complex numbers

    public static complexAdd sum(complexAdd n1, complexAdd n2) {

        return new complexAdd(n1.realNo + n2.realNo, n1.imgNo + n2.imgNo);

    }

     //Display function definition

    public void display() {

        System.out.printf("%.1f + %.1fi\n", realNo, imgNo);

    }

     public static void main(String[] args) {

        complexAdd n1 = new complexAdd(3.5, 4.1);

        complexAdd n2 = new complexAdd(5.4, 6.0);

         complexAdd result = complexAdd.sum(n1, n2);

        System.out.print("Sum = ");

        result.display();

    }

}

Save this program as “complexAdd.java”. Compile and run the program. You get the below output.

If you want to subtract the complex numbers, change the coding as below….

class complexSubtract {

    double realNo;

    double imgNo;

    // Constructor to initialize the complex number

    public complexSubtract(double realNo, double imgNo) {

        this.realNo = realNo;

        this.imgNo = imgNo;

    }

    // Static method to subtract two complex numbers

    public static complexSubtract subtract(complexSubtract n1, complexSubtract n2) {

        return new complexSubtract(n1.realNo - n2.realNo, n1.imgNo - n2.imgNo);

    }

    // Method to display the complex number

    public void display() {

        System.out.printf("%.1f + %.1fi\n", realNo, imgNo);

    }

    public static void main(String[] args) {

        complexSubtract n1 = new complexSubtract(6.5, 8.1);

        complexSubtract n2 = new complexSubtract(3.4, 6.0);

        complexSubtract result = complexSubtract.subtract(n1, n2);

        System.out.print("Sub = ");

        result.display();

    }

}

When you compile and execute this program, this will give you the subtraction of two real numbers.

These are ways to use the complex numbers. Generally, complex numbers are used in digital signal processing and cryptography.

No comments:

Post a Comment