-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultMatrix.java
66 lines (54 loc) · 1.76 KB
/
MultMatrix.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Write a program to multiply two matrices */
import java.util.Scanner;
class MultMAtrix{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows in 1st matrix : ");
int row1 = sc.nextInt();
System.out.print("Enter the number of columns in 1st matrix : ");
int cols1 = sc.nextInt();
System.out.print("Enter the number of rows in 2nd matrix : ");
int row2 = sc.nextInt();
System.out.print("Enter the number of columns in 2nd matrix : ");
int cols2 =sc.nextInt();
if(cols1 != row2){
System.out.print("Matrix multiplication is not possible. Column of first matrix should be equal to the row of the second matrix.");
return;
}
int[][] matrix1 = new int[row1][cols1];
int[][] matrix2 = new int[row2][cols2];
int[][] product = new int[row1][cols2];
///first matrix
System.out.println("Enter values for first matrix : ");
for(int i=0;i<row1;i++){
for(int j=0;j<cols1;j++){
System.out.print("Enter element at position [" + i + "][" + j + "]: ");
matrix1[i][j] = sc.nextInt();
}
}
//Second matrix
System.out.println("Enter values for second matrix : ");
for(int i=0;i<row2;i++){
for(int j=0;j<cols2;j++){
System.out.print("Enter element at position [" + i + "][" + j + "]: ");
matrix2[i][j] = sc.nextInt();
}
}
//Multiplication
for(int i=0;i<row1;i++){
for(int j=0;j<cols2;j++){
for(int k=0;k<cols1;k++){
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
//Display
System.out.println("Matrix after multiplication : ");
for(int i=0;i<row1;i++){
for(int j=0;j<cols2;j++){
System.out.print(product[i][j]+" ");
}
System.out.println();
}
}
}