BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Add Two Complex Numbers

Last Updated: September 7, 2018 by Chaitanya Singh | Filed Under: Java Examples

Complex numbers have two parts – real part and imaginary part. In this tutorial, we will write a Java program to add two complex numbers. When adding complex numbers we add real parts together and imaginary parts together as shown in the following diagram.

Java Add two complex numbers

Example – Adding two complex numbers in Java

In this program we have a class ComplexNumber. In this class we have two instance variables real and img to hold the real and imaginary parts of complex numbers.

We have declared a method sum() to add the two numbers by adding their real and imaginary parts together.

The constructor of this class is used for initializing the complex numbers. For e.g. when we create an instance of this class like this ComplexNumber temp = new ComplexNumber(0, 0);, it actually creates a complex number 0 + 0i.

public class ComplexNumber{
   //for real and imaginary parts of complex numbers
   double real, img;
	
   //constructor to initialize the complex number
   ComplexNumber(double r, double i){
	this.real = r;
	this.img = i;
   }
	
   public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2)
   {
	//creating a temporary complex number to hold the sum of two numbers
        ComplexNumber temp = new ComplexNumber(0, 0);

        temp.real = c1.real + c2.real;
        temp.img = c1.img + c2.img;
        
        //returning the output complex number
        return temp;
    }
    public static void main(String args[]) {
	ComplexNumber c1 = new ComplexNumber(5.5, 4);
	ComplexNumber c2 = new ComplexNumber(1.2, 3.5);
        ComplexNumber temp = sum(c1, c2);
        System.out.printf("Sum is: "+ temp.real+" + "+ temp.img +"i");
    }
}

Output:

Sum is: 6.7 + 7.5i

Screenshot: Same Java program in Eclipse IDE –
Java Program to add two complex numbers in Eclipse IDE

Output in Eclipse IDE:
Output of adding two complex numbers in Java
Here are a few related java examples:

    1. Java program to find largest of three numbers
    2. Java program to make a calculator using Switch Case
    3. Java program to swap two numbers
    4. C++ program to add two complex numbers

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program to Check two Strings are anagram or not
  3. Java Program to Calculate Power of a Number
  4. Java Program to reverse the Array
  5. Java Program to check if Number is Positive or Negative

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap