program to read the elements of the given two matrices of order n*n and to perform the matrix multiplication.
Hi Friend,
Try the following code:
import java.util.*; class MatrixMultiplication{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int[][] A = new int[3][3]; int[][] B = new int[3][3]; int[][] C = new int[3][3]; System.out.println("Enter elements for matrix A : "); for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = input.nextInt(); } System.out.println("Enter elements for matrix B : "); for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = input.nextInt(); } System.out.println("Matrix A: "); for (int i=0 ; i < A.length ; i++){ System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Matrix B: "); for (int i=0 ; i < B.length ; i++){ System.out.println(); for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Result is: "); System.out.println(); for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ for(int k=0;k<3;k++){ C[i][j]+=A[i][k]*B[k][j]; } } } for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(+C[i][j]+" "); } System.out.println(); } } }
Thanks
Hi Friend,
Try the following code:
import java.util.*; class MatrixMultiplication{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int[][] A = new int[3][3]; int[][] B = new int[3][3]; int[][] C = new int[3][3]; System.out.println("Enter elements for matrix A : "); for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = input.nextInt(); } System.out.println("Enter elements for matrix B : "); for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = input.nextInt(); } System.out.println("Matrix A: "); for (int i=0 ; i < A.length ; i++){ System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Matrix B: "); for (int i=0 ; i < B.length ; i++){ System.out.println(); for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Result is: "); System.out.println(); for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ for(int k=0;k<3;k++){ C[i][j]+=A[i][k]*B[k][j]; } } } for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(+C[i][j]+" "); } System.out.println(); } } }
Thanks
import java.util.*; import java.io.*; import java.math.*; class u1 { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Square matrix size"); int u=Integer.parseInt(br.readLine()); int f=Integer.parseInt(br.readLine()); int u1[][]=new int[u][f]; int f1[][]=new int[u][f]; int uf[][]=new int[u][f]; System.out.println("Enter 1'st matrix values"); for(int i=0;i<u;i++) { for(int j=0;j<f;j++) { u1[i][j]=Integer.parseInt(br.readLine()); } } System.out.println("Enter 2'nd matrix values"); for(int i=0;i<u;i++) { for(int j=0;j<f;j++) { f1[i][j]=Integer.parseInt(br.readLine()); } } System.out.println("Matrix A is:"); for(int i=0;i<u;i++) { for(int j=0;j<f;j++) { System.out.print(" "+u1[i][j]); } System.out.println(""); System.out.println(""); } System.out.println(""); System.out.println("Matrix B is:"); for(int i=0;i<u;i++) { for(int j=0;j<f;j++) { System.out.print(" "+f1[i][j]); } System.out.println(""); System.out.println(""); } System.out.println(""); System.out.println("Done by [email protected]"); System.out.println("Multiplication of Matrix is:"); for(int i=0;i<u;i++) { for(int j=0;j<f;j++) { for(int k=0;k<f;k++) { uf[i][j]+=u1[i][k]*f1[k][j]; } } } for(int i=0;i<u;i++) { for(int j=0;j<f;j++) { System.out.print(" "+uf[i][j]); } System.out.println(""); System.out.println(""); } } }
Ads