Program to add and Subtract Complex Numbers using Class in Java

In this article, we will try to add and subtract these two Complex Numbers by creating a Class for Complex Numbers, in which:

Example:

Input: a1 = 4, b1 = 8 a2 = 5, b2 = 7 Output: Sum = 9 + i15 Difference = -1 + i Explanation: (4 + i8) + (5 + i7) = (4 + 5) + i(8 + 7) = 9 + i15 (4 + i8) - (5 + i7) = (4 - 5) + i(8 - 7) = -1 - i Input: a1 = 9, b1 = 3 a2 = 6, b2 = 1 Output: Sum = 15 + i4 Difference = 3 + 2i

Java

// Java program to add and subtract two // complex numbers using Class import java.util.*; // User Defined Complex class class Complex < // Declaring variables int real, imaginary; // Empty Constructor // Constructor to accept // real and imaginary part Complex( int tempReal, int tempImaginary) real = tempReal; imaginary = tempImaginary; // Defining addComp() method // for adding two complex number Complex addComp(Complex C1, Complex C2) // creating temporary variable Complex temp = new Complex(); // adding real part of complex numbers temp.real = C1.real + C2.real; // adding Imaginary part of complex numbers temp.imaginary = C1.imaginary + C2.imaginary; // returning the sum return temp; // Defining subtractComp() method // for subtracting two complex number Complex subtractComp(Complex C1, Complex C2) // creating temporary variable Complex temp = new Complex(); // subtracting real part of complex numbers temp.real = C1.real - C2.real; // subtracting Imaginary part of complex numbers temp.imaginary = C1.imaginary - C2.imaginary; // returning the difference return temp; // Function for printing complex number void printComplexNumber() System.out.println("Complex number: " + imaginary + "i"); // Main Class public class GFG < // Main function public static void main(String[] args) // First Complex number Complex C1 = new Complex( 3 , 2 ); // printing first complex number C1.printComplexNumber(); // Second Complex number Complex C2 = new Complex( 9 , 5 ); // printing second complex number C2.printComplexNumber(); // for Storing the sum Complex C3 = new Complex(); // calling addComp() method C3 = C3.addComp(C1, C2); // printing the sum System.out.print("Sum of "); C3.printComplexNumber(); // calling subtractComp() method C3 = C3.subtractComp(C1, C2); // printing the difference System.out.print("Difference of "); C3.printComplexNumber(); Output:
Complex number: 3 + 2i Complex number: 9 + 5i Sum of Complex number: 12 + 7i Difference of Complex number: -6 + -3i

Time Complexity: O(1)
Auxiliary Space: O(1)

Like Article -->

Please Login to comment.

Similar Reads

Java Program to Add two Complex Numbers

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 nu

3 min read Java Program To Subtract Two Numbers Represented As Linked Lists

Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller from the larger ones.It may be assumed that there are no extra leading zeros in input lists.Examples: Inpu

5 min read Program to Add Two Complex Numbers

Given two complex numbers of the form and the task is to add these two complex numbers. [Tex]a_ <1>+ ib_ <1>[/Tex]and [Tex]a_ + ib_ [/Tex] Here the values of real and imaginary numbers are passed while calling the parameterized constructor and, with the help of a default(empty) constructor, the function addComp is called to get the addition o

7 min read Java Program to Subtract the Two Matrices

Given below the two matrices A and B, the task is to subtract them, and for subtracting the matrices the size of both the matrix should be the same i.e. both the matrix must have N X M dimension. Example: Input : A[][] = <<3, 1>, > B[][] = , >Output: , >Input : A[][] = , > B[][] = , > Output 3 min read BigInteger subtract() Method in Java with Examples

The java.math.BigInteger.subtract(BigInteger val) is used to calculate the Arithmetic difference of two BigIntegers. This method is applicable on large value numbers of range much greater than the range of biggest data type double of Java without compromising with the precision of the result but as BigInteger class internally uses array of integers

3 min read BigDecimal subtract() Method in Java with Examples

The java.math.BigDecimal.subtract(BigDecimal val) is used to calculate the Arithmetic difference of two BigDecimals. This method is used to find the arithmetic difference of large numbers without compromising with the precision of the result. This method performs an operation upon the current BigDecimal by which this method is called and BigDecimal

3 min read Java Program to Add Two numbers Without using Arithmetic Operator

Here, we need to write a function that returns the sum of two stated integers. And the function must not utilize any of the arithmetic operators such as +, ++, –, -, .. Etc.). It will be a very basic elementary level program to compute the sum by simply using the '+' operator and thereby simply printing and displaying out the resultant. But if we r

2 min read How to Perform a 2D FFT Inplace Given a Complex 2D Array in Java?

A fast Fourier transform (FFT) is an algorithm to compute the discrete Fourier transform (DFT) and its inverse. Fourier analysis converts time (or space) to the frequency and vice versa. FFT reduces the computation time required to compute a discrete Fourier Transform and improves the performance by a factor 100 or more over direct evaluation of DF

5 min read Java Program To Add Two Numbers Represented By Linked Lists- Set 1

Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input: List1: 5->6->3 // represents number 563 List2: 8->4->2 // represents number 842 Output: Resultant list: 1->4->0->5 // represents number 1405 Explanation: 563 + 842 = 1405

4 min read Java Program to Add Two Numbers

Given two integers num1 and num2, the task is to find the sum of the given two numbers in Java. Example of Addition of Two Numbers Input: A = 5, B = 6Output: sum = 11 Input: A = 4, B = 11 Output: sum = 15 Program to Add Two Numbers in Java Below is the implementation of adding two Numbers are mentioned below: Java Code // Java Program to implemen

4 min read Java Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N

Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = <1, 2, 3, 4, 5, 6, 7, 8>Output: Explanation: Even element =

2 min read Java Program to Add Two Matrix Using Iterative Approach

Given two matrices A and B, Add two matrices using iterative approaches in Java, like for loop, while loop, do-while loop. For the addition of two matrices, the necessary condition is that both the matrices must have the same size, and addition must take complete iteration to both the matrices. Examples Input: A[][] = <<1, 3>, > B[][] =

2 min read Java Program to Add an Element to ArrayList using ListIterator

In this article, we will learn how to add an element to ArrayList using ListIterator. The ListIterator is used to return a list iterator over the list elements. The listIterator() returns an iterator over the list from the beginning, but the listIterator(index) returns an iterator over the list from the given index. Syntax: // listIterator() method

2 min read How to Add Suffix and Prefix to The Label & Add Legend to Graph in Android?

When we are looking for a view to represent some statistical data or looking for a UI for displaying a graph in your app so in this article, we will take a look at creating a LineGraphView and adding and prefix to the label of the graph in our Android App. In this article, we will be building a simple Line Graph View in our Android app and we will

4 min read Java Program to Check if a Given Class is a Local Inner Class

Local Inner Classes are classes declared inside a block. These classes are only visible inside the block. So, You need to instantiate the class within the block. Sometimes, this block can be for loop or if-else clause. These classes can access the fields of a class enclosing it. The local inner class must be instantiated in the block they are defin

5 min read Java Program to Check if a Given Class is an Anonymous Class

Anonymous classes are the inner classes with no name. We can't create the instances of this anonymous class since they have no name. Anonymous inner class are mainly created in two ways: Class (may be abstract or concrete)Interface Syntax: The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a cl

5 min read

Java Program to Illustrate the Availability of Default Constructor of the Super Class to the Sub Class by Default

A constructor in Java is a special method that is used to initialize an object. Whenever an object is created using the new() keyword at least one construction is called. The constructor name must match with the class name and cannot have a return type. If there is no constructor available in the class in such a case java compiler provides a defaul

5 min read Java Program to Check if a Given Class is an Inner Class

Inner class is a member of another class which is basically a non-static nested class i.e. if a class is inside another class and is not static, then the class is called is referred to as an inner class. Types of Inner Classes: Nested Inner classMethod Local inner classesAnonymous inner classesStatic nested classes Approach 1: First check if the gi

5 min read Java.lang.Class class in Java | Set 1

Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons

15+ min read Java.lang.Class class in Java | Set 2

Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo

15+ min read How to Add Custom Class Objects to the TreeSet in Java?

TreeSet is an implementation of the SortedSet interface in java that uses a red-black tree for storage. By default, It maintains an ascending order. It contains unique elements only. It doesn't allow null elements. Access and retrieval times are quite fast. To add the user-defined object into TreeSet, we need to implement a Comparable interface. An

3 min read Java Program to Split the array and add the first part to the end

There is a given an array and split it from a specified position, and move the first part of array add to the end. Examples: Input : arr[] = <12, 10, 5, 6, 52, 36>k = 2 Output : arr[] = Explanation : Split from index 2 and first part <12, 10>add to the end . Input : arr[] = k = 1 Output : arr[] = Explana

2 min read Java Program to Split the array and add the first part to the end | Set 2

Given an array and split it from a specified position, and move the first part of array add to the end. Examples: Input : arr[] = <12, 10, 5, 6, 52, 36>k = 2 Output : arr[] = Explanation : Split from index 2 and first part <12, 10>add to the end . Input : arr[] = k = 1 Output : arr[] = Explanation :

2 min read Java Program to Find GCD and LCM of Two Numbers Using Euclid’s Algorithm

GCD or the Greatest Common Divisor of two given numbers A and B is the highest number dividing both A and B completely, i.e., leaving remainder 0 in each case. LCM or the Least Common Multiple of two given numbers A and B is the Least number which can be divided by both A and B, leaving remainder 0 in each case. The LCM of two numbers can be comput

3 min read Implement Pair Class with Unit Class in Java using JavaTuples

To implement a Pair class with a Unit class in Java using JavaTuples, you can use the Pair class provided by the library and create a new Unit class that extends the Unit class provided by the library. Here is an example implementation: Here is an example Java code that uses the MyPair class with MyUnit and displays the output: Java Code import org

4 min read Implement Triplet Class with Pair Class in Java using JavaTuples

Following are the ways to implement Triplet Class with Pair Class Using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // create Pair Pair<Integer, String> pair = new Pair<Integer, String>( Integer.valueOf(1), "GeeksforGeeks"); // Print the Pair System.out.printl

2 min read Implement Quintet Class with Quartet Class in Java using JavaTuples

Following are the ways to implement Quintet Class with Quartet Class Using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // create Quartet Quartet<String, String, String, String> quartet = new Quartet<String, String, String, String>( "Quartet", "Triplet

4 min read Implement Quartet Class with Triplet Class in Java using JavaTuples

Following are the ways to implement Quartet Class with Triplet Class Using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // create Triplet Triplet<String, String, String> triplet = new Triplet<String, String, String>( "Triplet 1", "1", "GeeksforGe

3 min read Implement Octet Class from Septet Class in Java using JavaTuples

Prerequisite: Octet Class, Septet Class Below are the methods to implement a Octet Class using Septet Class in Java: Using direct values // Java program to illustrate // implementing Octet Class // from Septet Class // using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // Create Sep

6 min read Implement Ennead Class from Octet Class in Java using JavaTuples

Prerequisite: Ennead Class, Octet Class Below are the methods to implement a Ennead Class using Octet Class in Java: Using direct values // Java program to illustrate // implementing Ennead Class // from Octet Class // using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // Create Oct