mathajax

Complex number Addition and Subtraction

The arithmetic operations addition or subtraction is carried out on the complex numbers by adding or subtracting on respective parts (real and imaginary) of them.


complex number addition and subtraction

Complex numbers Addition

Add two complex numbers z1 and z2

      z1  = a + bi
      z2  = c + di

      z1 + z2   =  a + bi + c +di
                =  (a+c)  + (b+d)i
                    
 

Complex numbers Subtraction

subtract complex numbers z2 from z2

 
        z1  = a + bi
        z2  = c + di

        z1 - z2   =  (a + bi) - (c+di)
                  =  a + bi    -c - di
                  =  (a-c)  + (b-d)i

Java code - Complex Addition and Subtraction

The following Java codes add and subtract two complex numbers and returns a complex number,as result of the operation.


public class AddSub {

   public static Complex subtract(Complex C1,Complex C2) 
    {  
  Complex tmp = new Complex();
  tmp.real = C1.real - C2.real;
  tmp.imag = C1.imag - C2.imag;
  return tmp;
    }

   public static Complex add(Complex C1,Complex C2) 
     {
  
  Complex tmp = new Complex();
  tmp.real = C1.real + C2.real;
  tmp.imag = C1.imag + C2.imag;
  return tmp;
     }

   public static void main(String[] args) {

     System.out.println("\nArithmetic Operation on Complex Number");
     System.out.println("\nComplex Number Addition");

     Complex C1 = new Complex(2,-3);
     Complex C2 = new Complex(-4,2);  
     System.out.println("Complex  C1: " +C1);    
     System.out.println("Complex  C2: " +C2);      
     Complex C3 =AddSub.add(C1,C2);  
     System.out.println("C1 + C2 =: " +C3);
  
     System.out.println("\nComplex Number Subtraction");
     Complex C4 = new Complex(-2,-2);
     Complex C5 = new Complex(3,-2);  
     System.out.println("Complex  C1: " +C4);    
     System.out.println("Complex  C2: " +C5);
  
     Complex C6=AddSub.subtract(C4,C5);  
     System.out.println("Complex C1-C2= " +C6);
  

  }

}


Output


Arithmetic Operation on Complex Number

Complex Number Addition
Complex  C1: 2.0-3.0i
Complex  C2: -4.0+2.0i
C1 + C2 =: -2.0-1.0i

Complex Number Subtraction
Complex  C1: -2.0-2.0i
Complex  C2: 3.0-2.0i
Complex C1-C2= -5.0-0.0i

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Chebyshev distance between two points

Binary 1's and 2's Complement