-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.java
48 lines (42 loc) · 1.73 KB
/
matrix.java
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
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
public class matrix {
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.println("Enter row and column of first matrix: ");
int r1 = s.nextInt();
int c1 = s.nextInt();
System.out.println("Enter row and column of second matrix: ");
int r2 = s.nextInt();
int c2 = s.nextInt();
if(c1 != r2)
System.out.println("Matrix multiplication not possible");
else
{
int a[][] = new int[10][10];
int b[][] = new int[10][10];
int c[][] = new int[10][10];
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
System.out.println("Enter the ["+i+"]["+j+"] of first matrix:");
a[i][j]=s.nextInt();
}
}
for(int i=0;i<r2;i++){
for(int j=0;j<c2;j++){
System.out.println("Enter the ["+i+"]["+j+"] of second matrix:");
b[i][j]=s.nextInt();
}
}
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
c[i][j]=0;
for(int k=0;k<c1;k++){
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
}