-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.java
More file actions
43 lines (36 loc) · 1.21 KB
/
Copy pathmatrix.java
File metadata and controls
43 lines (36 loc) · 1.21 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
38
39
40
41
42
43
import java.util.Scanner;
public class matrix{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of rows of matrix a");
int rowa=sc.nextInt();
System.out.println("enter the number of columns of matrix a");
int cola=sc.nextInt();
System.out.println("enter the number of rows of matrix b");
int rowb=sc.nextInt();
System.out.println("enter the number of columns of matrix b");
int colb=sc.nextInt();
int[][] matrixa=new int[rowa][cola];
int[][] matrixb=new int[rowb][colb];
int[][] result=new int[rowa][colb];
System.out.println("enter the elements of matrix a:");
for(int i=0;i<rowa;i++)
for(int j=0;j<cola;j++)
matrixa[i][j]=sc.nextInt();
System.out.println("enter the elements of matrix b:");
for(int i=0;i<rowb;i++)
for(int j=0;j<colb;j++)
matrixb[i][j]=sc.nextInt();
for(int i=0;i<rowa;i++)
for(int j=0;j<colb;j++){
result[i][j]=0;
for(int k=0;k<cola;k++)
result[i][j]+=matrixa[i][k]*matrixb[k][j];
}
System.out.println("resultant matrix");
for(int i=0;i<rowa;i++){
for(int j=0;j<colb;j++)
System.out.print(result[i][j]+" ");
}
}
}