Java Program to Add Two Complex Numbers

This is the Java Program to Add Two Complex Numbers.

Problem Description

Given two complex numbers, write a java program to add the two numbers.

Problem Solution

Add the real and imaginary parts of the numbers seperately and store them in a new variable.

Program/Source Code

Here is the source code of the Java Program to Add Two Complex Numbers. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Add Two Complex Numbers
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class ComplexNumbers {
  8.     // Main function to read two complex numbers and add them
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         double i1,j1,i2,j2;
  12.         System.out.println("Enter the real part and
  13.                             imaginary part of the first complex number");
  14.         try{
  15.             i1=Double.parseDouble(br.readLine());
  16.             j1=Double.parseDouble(br.readLine());
  17.         }catch (Exception e){
  18.             System.out.println("An error occurred");
  19.             return;
  20.         }
  21.         System.out.println("Enter the real part and
  22.                             imaginary part of the second complex number");
  23.         try{
  24.             i2=Double.parseDouble(br.readLine());
  25.             j2=Double.parseDouble(br.readLine());
  26.         }catch (Exception e){
  27.             System.out.println("An error occurred");
  28.             return;
  29.         }
  30.         System.out.println("The first complex number is "
  31.                             + i1 + " + i(" + j1 + ")");
  32.         System.out.println("The second complex number is "
  33.                             + i2 + " + i(" + j2 + ")");
  34.         System.out.println("The sum of the two complex numbers is "
  35.                             + (i1 + i2) + " + i(" + (j1 + j2) + ")");
  36.     }
  37. }
Program Explanation

1. In function main(), the real parts of the complex number are stored in i1 and i2 and the imaginary parts are stored in j1 and j2.
2. Then, in the third println statement real parts are added separately and imaginary parts are added separately.

advertisement
advertisement

Time Complexity: O(1).

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the real part and imaginary part of the first complex number
4
6
Enter the real part and imaginary part of the second complex number
-5
2
The first complex number is 4.0 + i(6.0)
The second complex number is -5.0 + i(2.0)
The sum of the two complex numbers is -1.0 + i(8.0)

Sanfoundry Global Education & Learning Series – Java Programs..

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.