Homework Help

write a java program to multiply two given matrices and display the resultant...

user profile pic

utkr940 | Student, Grade 11 | Honors

Posted December 14, 2010 at 10:52 PM via web

dislike 0 like

write a java program to multiply two given matrices and display the resultant matrix.

its a java program to be written in BlueJ platform.

1 Answer | Add Yours

user profile pic

satavisha | Student, Grade 9 | eNoter

Posted February 8, 2011 at 3:15 PM (Answer #1)

dislike 0 like

Here is the program on Multiplication of two matrices


import java.io.*;class smith1{int i, j, k, l, m, n, o;int mat1[][], mat2[][], mat3[][];BufferedReader br=new BufferedReader(new InputStreamReader(System.in));public void take() throws Exception{System.out.println("Program for Multiplication of matrix.\n\n");System.out.println("Enter row of 1st matrix (1 to 50,1 to 50) : ");k=Integer.parseInt(br.readLine());System.out.println("Enter column of 1st matrix (1 to 50,1 to 50) : ");l=Integer.parseInt(br.readLine());mat1=new int[k][l];mat3=new int[k][l];System.out.println("\nEnter elements of 1st matrix\n");for(i = 0; i < k; i++){for(j = 0; j < l; j++)mat1[i][j]=Integer.parseInt(br.readLine());}System.out.println("Enter row of 2nd matrix (1 to 50,1 to 50) : ");m=Integer.parseInt(br.readLine());System.out.println("Enter column of 2nd matrix (1 to 50,1 to 50) : ");n=Integer.parseInt(br.readLine());mat2=new int[m][n];System.out.println("\nEnter elements of 2nd matrix\n");for(i = 0; i < m; i++){for(j = 0; j < n; j++)mat2[i][j]=Integer.parseInt(br.readLine());}if (l == m){for(i = 0; i < k; i++){for(j = 0; j < n; j++){mat3[i][j] = 0;for(o = 0; o < n; o++)mat3[i][j] = mat3[i][j] + mat1[i][j] * mat2[i][o];}}}else{System.out.println("\nMultiplication not possible\n");}System.out.println("\n\t*** Matrix 1 ***\n");for(i = 0; i < k; i++){for(j = 0; j < l; j++)System.out.print(" "+ mat1[i][j]);System.out.println();}System.out.println("\n\t*** Matrix 2 ***\n");for(i = 0; i < k; i++){for(j = 0; j < l; j++)System.out.print(" "+ mat2[i][j]);System.out.println();}System.out.println("\n\t*** Result ***\n");for(i = 0; i < k; i++){for(j = 0; j < l; j++)System.out.print(" "+ mat3[i][j]);System.out.println();}
}public static void main(String args[]) throws Exception{ smith1 obj=new smith1();obj.take();}}

Join to answer this question

Join a community of thousands of dedicated teachers and students.

Join eNotes