Java Program for addition and subtraction of Complex Numbers

In this tutorial of java programming language, we will learn how to make a java program for addition and subtraction of complex numbers. So if you are looking for source code of addition and subtraction program of complex numbers, you have landed at best place.

Source Code of Java Program for addition and subtraction of Complex Numbers

//Java Program for addition and subtraction of complex numbers

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class ComplexNumbers {

   public static void main(String args[])
   throws IOException
   {

     BufferedReader in = new  BufferedReader
     (new InputStreamReader(System.in));

     int a, b, c, d;

     System.out.println("Program to add two complex numbers..");

     System.out.println("\n"+"Enter first number a+bi");

     System.out.print("Enter value for a: ");
     a = Integer.parseInt(in.readLine());

     System.out.print("Enter value for b: ");
     b = Integer.parseInt(in.readLine());

     System.out.println("\n"+"Enter second number c+di");

     System.out.print("Enter value for c: ");
     c = Integer.parseInt(in.readLine());

     System.out.print("Enter value for d: ");
     d = Integer.parseInt(in.readLine());

     System.out.println("\n"+"Result of addtion");
     System.out.print(a+c);
     System.out.print((b+d)>0?"+":"");
     System.out.print(b+d + "i ");

     System.out.println("\n"+"Result of subtraction");
     System.out.print(a-c);
     System.out.print((b+d)>0?"+":"");
     System.out.print(b-d + "i ");
   }
}

Output of java program for addition and subtraction of complex numbers

What are Complex Numbers ?

A complex number is a combination of real numbers and imaginary numbers, which can be expressed in the form of a+bi, where a and b are the real numbers and i is the imaginary part of the complex number.
Examples of Complex Numbers : 1+i, 12+ 25i, 1+3i etc. and where "i" is the unit imaginary number and is equalt to the square root of -1.

How to make java program for complex numbers?

To make a Java program for addition, subtraction or multiplication of complex numbers, we just need to implement the mathematical way or formula of addition, subtraction or multiplication of complex numbers in programming language. For example, for the addition of two complex numbers in any programming language , we have to implement this mathematical formula in our program or source code for addition of complex numbers:
(a+bi) + (c+di) = (a+c) + (b+d)i

0 comments:

Post a Comment