-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplication of 2D matrix.java
More file actions
37 lines (37 loc) · 1 KB
/
Multiplication of 2D matrix.java
File metadata and controls
37 lines (37 loc) · 1 KB
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
import java.util.*;
public class sumoftwomatrix{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int p=sc.nextInt();
int q=sc.nextInt();
if(m==p && n==q){
int[][] arr1=new int[m][n];
int[][] arr2=new int[p][q];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
arr1[i][j]=sc.nextInt();
}
}
for(int i=0;i<p;i++){
for(int j=0;j<q;j++){
arr2[i][j]=sc.nextInt();
}
}
int c[][]=new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
c[i][j]=0;
for(int k=0;k<m;k++){
c[i][j]=arr1[i][k]+arr2[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}else{
System.out.println("not possible");
}
}
}