A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-program-to-add-two-complex-numbers/ below:

Java Program to Add two Complex Numbers

Java Program to Add two Complex Numbers

Last Updated : 13 Jul, 2022

Complex numbers are numbers that consist of two parts — a real number and an imaginary number. Complex numbers are the building blocks of more intricate math, such as algebra. The standard format for complex numbers is a + bi, with the real number first and the imaginary number last.

General form for any complex number is:

a+ib

Where "a" is real number and "b" is Imaginary number.

Construction of Complex number

For creating a complex numbers, we will pass imaginary numbers and real numbers as parameters for constructors.

Java
// Java program to construct the complex number

class ComplexNumber {

    // variables to hold real and imaginary part of complex
    // number
    int real, image;

    // Constructor which will be used while creating complex
    // number
    public ComplexNumber(int r, int i)
    {
        this.real = r;
        this.image = i;
    }

    // function to print real number
    public void showC()
    {
        System.out.println(this.real + " +i " + this.image);
    }

    // we will implement  this function for addition
    public complex add(ComplexNumber, ComplexNumber);
}

Time Complexity: O(1)

Auxiliary Space: O(1)

 
Add function

Ex. addition of two complex numbers

(a1) + (b1)i -----(1)

(a2)+ (b2)i -----(2)

adding (1) and (2) will look like

(a1+a2) + (b1+b2)i

Function Definition: 

ComplexNumber add(ComplexNumber n1, ComplexNumber n2){
    
  ComplexNumber res = new ComplexNumber(0,0); //creating blank complex number 
  
  // adding real parts of both complex numbers
  res.real = n1.real + n2.real;
  
  // adding imaginary parts
  res.image = n1.image + n2.image;
  
  // returning result
  return res;

}

 
 Code: 

Java
// Java program to add two complex numbers

class ComplexNumber {

    // variables to hold real and imaginary part of complex
    // number
    int real, image;

    // Constructor which will be used while creating complex
    // number
    public ComplexNumber(int r, int i)
    {
        this.real = r;
        this.image = i;
    }

    // function to print real number
    public void showC()
    {
        System.out.print(this.real + " +i" + this.image);
    }

    // function for addition
    public static ComplexNumber add(ComplexNumber n1,
                                    ComplexNumber n2)
    {

        // creating blank complex number
        // to store result
        ComplexNumber res = new ComplexNumber(0, 0);

        // adding real parts of both complex numbers
        res.real = n1.real + n2.real;

        // adding imaginary parts
        res.image = n1.image + n2.image;

        // returning result
        return res;
    }

    public static void main(String arg[])
    {

        // creating two complex numbers
        ComplexNumber c1 = new ComplexNumber(4, 5);
        ComplexNumber c2 = new ComplexNumber(10, 5);

        // printing complex numbers
          System.out.print("first Complex number: ");
        c1.showC();
        
        System.out.print("\nSecond Complex number: ");
        c2.showC();

        // calling add() to perform addition
        ComplexNumber res = add(c1, c2);

        // displaying addition
        System.out.println("\nAddition is :");
        res.showC();
    }
}

Output
first Complex number: 4 +i5
Second Complex number: 10 +i5
Addition is :
14 +i10

Time Complexity: O(1)

Auxiliary Space: O(1)



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4