import java.util.*; class LabTask13 { static int i,j,r1,c1,r2,c2,k; public static void main(String... args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the no.of rows of 1st matrix: "); r1=sc.nextInt(); System.out.print("Enter the no.of columns of 1st matrix: "); c1=sc.nextInt(); System.out.print("Enter the no.of rows of 2nd matrix: "); r2=sc.nextInt(); System.out.print("Enter the no.of columns of 2nd matrix: "); c2=sc.nextInt(); if(c1!=r1) { System.out.println("matrix multiplication is not possible"); return ; } int mul[][]=new int[r1][c2]; System.out.println("Enter first matrix elements:"); int a[][]=new int[r1][c1]; for(i=0;i<r1;i++){ for(j=0;j<c1;j++){ a[i][j]=sc.nextInt(); } } System.out.println("Enter second matrix elements:"); int b[][]=new int[r2][c2]; for(i=0;i<r2;i++){ for(j=0;j<c2;j++){ b[i][j]=sc.nextInt(); } } System.out.println("Resultant matrix is:"); for(i=0;i<r1;i++){ for(j=0;j<c2;j++){ mul[i][j]=0; for(k=0;k<c1;k++){ mul[i][j]=a[i][k]*b[k][j]; } System.out.print(mul[i][j]+" "); } System.out.println(); }}}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter