-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplication.java
More file actions
70 lines (67 loc) · 2.09 KB
/
Copy pathMatrixMultiplication.java
File metadata and controls
70 lines (67 loc) · 2.09 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*********************************************************************************************
* File :MatrixMultiplication.java
* Description :To multiply two matrix using arrays
* Author :Shraya S Santhosh
* Version :1.0
* Date :3/10/2023
*********************************************************************************************/
package newproj;
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[]args) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter the order of the first matrix:");
int ROW_SIZE_1=sc.nextInt();
int COLUMN_SIZE_1=sc.nextInt();
int [][]matrix1=new int[ROW_SIZE_1][COLUMN_SIZE_1];
System.out.println("Enter the values of the first matrix:");
for (int i=0;i<ROW_SIZE_1;i++) {
for (int j=0;j<COLUMN_SIZE_1;j++) {
matrix1[i][j]=sc.nextInt();
}
}
System.out.println("Enter the order of the second matrix:");
int ROW_SIZE_2=sc.nextInt();
int COLUMN_SIZE_2=sc.nextInt();
int [][]matrix2=new int[ROW_SIZE_2][COLUMN_SIZE_2];
System.out.println("Enter the values of the second matrix:");
for (int i=0;i<ROW_SIZE_2;i++) {
for (int j=0;j<COLUMN_SIZE_2;j++) {
matrix2[i][j]=sc.nextInt();
}
}
System.out.println("First matrix:");
for (int i=0;i<ROW_SIZE_1;i++) {
for (int j=0;j<COLUMN_SIZE_1;j++) {
System.out.print(matrix1[i][j]+"\t");
}
System.out.println();
}
System.out.println("Second matrix:");
for (int i=0;i<ROW_SIZE_2;i++) {
for (int j=0;j<COLUMN_SIZE_2;j++) {
System.out.print(matrix2[i][j]+"\t");
}
System.out.println();
}
if (COLUMN_SIZE_1!=ROW_SIZE_2)
{
System.out.println("Multiplication not possible");
}
else
{
int [][]matrix3=new int[ROW_SIZE_1][COLUMN_SIZE_2];
System.out.println("Resultant Matrix is:");
for (int i=0;i<ROW_SIZE_1;i++) {
for (int j=0;j<COLUMN_SIZE_2;j++) {
matrix3[i][j]=0;
for (int k=0;k<COLUMN_SIZE_1;k++) {
matrix3[i][j]=matrix3[i][j]+matrix1[i][k]*matrix2[k][j];
}
System.out.print(matrix3[i][j]+"\t");
}
System.out.println();
}
}
}
}