VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-add-two-complex-numbers/

⇱ Java Program to Add two Complex Numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Add two Complex Numbers

Last Updated : 23 Jan, 2026

A complex number is a number that consists of two parts: a real part and an imaginary part. It is usually written in the form a + bi, where a is the real part and b is the imaginary part.

Example:

3 + 4i -> Real part = 3, Imaginary part = 4
5 - 2i -> Real part = 5, Imaginary part = -2

Formula to Add Two Complex Numbers

If we have two complex numbers:

  • z₁ = a + bi
  • zā‚‚ = c + di

Then their addition is calculated as:

z₁ + zā‚‚ = (a + c) + (b + d)i

Adding Two Complex Numbers

The addition of two complex numbers is done by adding the real parts and imaginary parts separately:


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

Explanation:

  • We create two complex numbers using the constructor.
  • add() method adds the real parts and imaginary parts separately and returns a new ComplexNumber.
  • showC() method prints the complex number in readable format.
  • Time Complexity: O(1) and Auxiliary Space: O(1)
Comment