Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- class matmul
- {
- public static void main(String args[])
- {
- int i,j,k,l,m,n;
- Scanner t = new Scanner (System.in);
- System.out.println("enter row and column of 1st matrix");
- int r1 = t.nextInt();
- int c1 = t.nextInt();
- System.out.println("enter row and column of 2nd matrix");
- int r2 = t.nextInt();
- int c2 = t.nextInt();
- if(c1==r2)
- {
- int a[][] = new int[r1][c1];
- int b[][] = new int[r2][c2];
- System.out.println("enter elements of 1st matrix");
- for(i=0;i<r1;i++)
- {
- for(j=0;j<c1;j++)
- {
- a[i][j] = t.nextInt();
- }
- }
- System.out.println("enter elements of 2nd matrix");
- for(i=0;i<r2;i++)
- {
- for(j=0;j<c2;j++)
- {
- b[i][j] = t.nextInt();
- }
- }
- int c[][] = new int[r1][c1];
- for(i=0;i<r1;i++)
- {
- for(j=0;j<r2;j++)
- {
- c[i][j]=0;
- for(k=0;k<c1;k++)
- {
- c[i][j]=c[i][j]+a[i][k]*b[k][j];
- }
- }
- }
- System.out.println("multiplication of two matrix is :");
- for(i=0;i<r1;i++)
- {
- for(j=0;j<c1;j++)
- {
- System.out.print(c[i][j]+" ");
- }
- System.out.println();
- }
- }
- else
- System.out.println("multiplication not possible");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement