We are going to share a Java Program to Perform Complex Number Multiplication. If you are a Java beginner and want to start learning the Java programming, then keep your close attention in this tutorial as I am going to share how to write a Java Program to Perform Complex Number Multiplication.
Copy the below Java program and execute it with the help of Javac compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import java.util.Scanner; public class Complex_Multiplication_Matrix { private double real=0.0, img=0.0; public Complex_Multiplication_Matrix(double real, double img) { this.real = real; this.img = img; } public Complex_Multiplication_Matrix() { this.real = 0; this.img = 0; } public Complex_Multiplication_Matrix complex_Form(double re, double im) { Complex_Multiplication_Matrix res = new Complex_Multiplication_Matrix(); res.real = re; res.img = im; return res; } public Complex_Multiplication_Matrix multiplication(Complex_Multiplication_Matrix C2) { Complex_Multiplication_Matrix Res = new Complex_Multiplication_Matrix(); Res.real = (this.real * C2.real) - (this.img * C2.img); Res.img = (this.real * C2.img) + (this.img * C2.real); return Res; } public Complex_Multiplication_Matrix addtion(Complex_Multiplication_Matrix C2) { Complex_Multiplication_Matrix Res = new Complex_Multiplication_Matrix(); this.real += C2.real; this.img += C2.img; Res.real = this.real; Res.img = this.img; return Res; } public Complex_Multiplication_Matrix[][] matrix_multiplication(Complex_Multiplication_Matrix[][] a, Complex_Multiplication_Matrix[][] b, Complex_Multiplication_Matrix[][] res, int n) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) res[i][j] = res[i][j].addtion(a[i][k].multiplication(b[k][j])); return res; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the dimension of the square matrix: "); int n = sc.nextInt(); double re,im; Complex_Multiplication_Matrix[][] a = new Complex_Multiplication_Matrix[n][n]; Complex_Multiplication_Matrix[][] b = new Complex_Multiplication_Matrix[n][n]; Complex_Multiplication_Matrix[][] res = new Complex_Multiplication_Matrix[n][n]; Complex_Multiplication_Matrix C = new Complex_Multiplication_Matrix(); System.out.println("Enter the complex elements of 1st matrix: "); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { re = sc.nextDouble(); im = sc.nextDouble(); a[i][j] = C.complex_Form(re, im); } } System.out.println("Enter the complex elements of matrix: "); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { re = sc.nextDouble(); im = sc.nextDouble(); b[i][j] = C.complex_Form(re, im); } } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { re = 0.0; im = 0.0; res[i][j] = C.complex_Form(re, im); } } res = C.matrix_multiplication(a, b, res, n); System.out.println("The Multiplication is:"); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) System.out.print(res[i][j].real+"+"+res[i][j].img+"i "); System.out.println(); } sc.close(); } } |
Enter the dimension of the square matrix:
2
Enter the complex elements of matrix:
1 2 1 2
1 2 1 2
Enter the complex elements of matrix:
1 2 1 2
1 2 1 2
The Multiplication is:
-6.0+8.0i -6.0+8.0i
-6.0+8.0i -6.0+8.0i
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.