-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixMultiply.java
More file actions
43 lines (42 loc) · 1.47 KB
/
Copy pathmatrixMultiply.java
File metadata and controls
43 lines (42 loc) · 1.47 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
//Question: Write a program to print the multiplication of two matrices given by users.
package twoDArray;
import java.util.Scanner;
public class matrixMultiply {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n1, m1, n2, m2;
System.out.print("Enter the number of rows and columns of the first array: ");
m1=sc.nextInt();
n1=sc.nextInt();
System.out.print("Enter the number of rows and columns of the second array: ");
m2=sc.nextInt();
n2=sc.nextInt();
if(n1==m2){
int[][] arr1=new int[m1][n1];
int[][] arr2=new int[m2][n2];
int[][] mul=new int[m1][n2];
System.out.println("Enter the elements of the first array: ");
for (int i = 0; i <m1 ; i++) {
for (int j = 0; j < n1; j++) {
arr1[i][j]=sc.nextInt();
}
}
System.out.println("Enter the elements of the second array: ");
for (int i = 0; i <m2 ; i++) {
for (int j = 0; j < n2; j++) {
arr2[i][j]=sc.nextInt();
}
}
for (int i = 0; i <m1; i++) {
for (int j = 0; j <n2; j++) {
mul[i][j] = 0;
for (int k = 0; k < n1; k++) {
mul[i][j] += arr1[i][k] * arr2[k][j];
}
System.out.print(mul[i][j]+" ");
}
System.out.println();
}
}else{System.out.println("Multiplication cannot be done!");}
}
}