Java Program to Add two Complex Numbers (original ) (raw )Last Updated : 13 Jul, 2022
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 number and "b" is Imaginary number.
Construction of Complex number
For creating a complex numbers, we will pass imaginary numbers and real numbers as parameters for constructors.
Javaclass
ComplexNumber {
`` int
real, image;
`` public
ComplexNumber(
int
r,
int
i)
`` {
`` this
.real = r;
`` this
.image = i;
`` }
`` public
void
showC()
`` {
`` System.out.println(
this
.real +
" +i "
+
this
.image);
`` }
`` public
complex add(ComplexNumber, ComplexNumber);
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Add function
Basically, addition of two complex numbers is done by adding real part of the first complex number with real part of the second complex number.
And adding imaginary part of the first complex number with the second which results into the third complex number.
So that means our add() will return another complex number.
Ex. addition of two complex numbers
(a1) + (b1)i —–(1)
(a2)+ (b2)i —–(2)
adding (1) and (2) will look like
(a1+a2) + (b1+b2)i
Function Definition:
ComplexNumber add(ComplexNumber n1, ComplexNumber n2){
ComplexNumber res = new ComplexNumber(0,0); //creating blank complex number
// adding real parts of both complex numbers
res.real = n1.real + n2.real;
// adding imaginary parts
res.image = n1.image + n2.image;
// returning result
return res;
}
Code:
Javaclass
ComplexNumber {
`` int
real, image;
`` public
ComplexNumber(
int
r,
int
i)
`` {
`` this
.real = r;
`` this
.image = i;
`` }
`` public
void
showC()
`` {
`` System.out.print(
this
.real +
" +i"
+
this
.image);
`` }
`` public
static
ComplexNumber add(ComplexNumber n1,
`` ComplexNumber n2)
`` {
`` ComplexNumber res =
new
ComplexNumber(
0
,
0
);
`` res.real = n1.real + n2.real;
`` res.image = n1.image + n2.image;
`` return
res;
`` }
`` public
static
void
main(String arg[])
`` {
`` ComplexNumber c1 =
new
ComplexNumber(
4
,
5
);
`` ComplexNumber c2 =
new
ComplexNumber(
10
,
5
);
`` System.out.print(
"first Complex number: "
);
`` c1.showC();
`` System.out.print(
"\nSecond Complex number: "
);
`` c2.showC();
`` ComplexNumber res = add(c1, c2);
`` System.out.println(
"\nAddition is :"
);
`` res.showC();
`` }
}
Output
first Complex number: 4 +i5
Second Complex number: 10 +i5
Addition is :
14 +i10
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Java Basic Programs
Java Program to Display All Prime Numbers from 1 to N For a given number N, the purpose is to find all the prime numbers from 1 to N. Examples: Input: N = 11Output: 2, 3, 5, 7, 11Input: N = 7Output: 2, 3, 5, 7 Approach 1: Firstly, consider the given number N as input.Then apply a for loop in order to iterate the numbers from 1 to N.At last, check if ea 4 min read
Java Program to Check Whether the Character is Vowel or Consonant In this article, we are going to learn how to check whether a character is a vowel or a consonant. So, the task is, for any given character, we need to check if it is a vowel or a consonant. As we know, vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ and all the other characters (i.e. ‘b’, ‘c’, ‘d’, ‘f’ …..) are 4 min read
Java Program to Find Sum of Fibonacci Series Numbers of First N Even Indexes For a given positive integer N, the purpose is to find the value of F2 + F4 + F6 +………+ F2n till N number. Where Fi indicates the i'th Fibonacci number. The Fibonacci Series is the numbers in the below-given integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ……Examples: Input: n = 4 Output: 3 4 min read
Java Pattern Programs
Java Conversion Programs
Java Classes and Object Programs
Java Methods Programs
Java Searching Programs
Java 1-D Array Programs
Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java. Example Input/Output: Input: arr = { 1, 2, 3, 4, 5}Output: 5 Input: arr = { 10, 3, 5, 7, 2, 12 4 min read
Java Program to Find Common Elements Between Two Arrays Given two arrays and our task is to find their common elements. Examples:Input: Array1 = ["Article", "for", "Geeks", "for", "Geeks"], Array2 = ["Article", "Geeks", "Geeks"]Output: [Article, Geeks] Input: Array1 = ["a", "b", "c", "d", "e", "f"], Array2 = ["b", "d", "e", "h", "g", "c"]Output: [b, c, d 4 min read
Java 2-D Arrays (Matrix) Programs
Java Program to Add two Matrices Given two matrices A and B of the same size, the task is to add them in Java. Examples: Input: A[][] = {{1, 2}, {3, 4}} B[][] = {{1, 1}, {1, 1}} Output: {{2, 3}, {4, 5}} Input: A[][] = {{2, 4}, {3, 4}} B[][] = {{1, 2}, {1, 3}} Output: {{3, 6}, {4, 7}} Program to Add Two Matrices in JavaFollow the St 2 min read
Java Program to Find Transpose of Matrix Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, the transpose of A[ ][ ] is obtained by changing A[i][j] to A[j][i]. Example of First Transpose of MatrixInput: [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ]Output: [ [ 1 , 4 , 7 ] , [ 2 , 5 , 8 ] 5 min read
Java Program to Find the Normal and Trace of a Matrix For a given 2D matrix, the purpose is to find the Trace and Normal of the matrix. In this article, we will learn about Normal and Trace of a Matrix in Java. Examples : Input : matrix[][] = {{1, 4, 4}, {2, 3, 7}, {0, 5, 1}}; Output : Normal = 11 Trace = 5 Explanation : Normal = sqrt(1*1+ 4*4 + 4*4 + 3 min read
Java String Programs