-
Notifications
You must be signed in to change notification settings - Fork 21
/
MatrixMultiplication.java
90 lines (88 loc) · 2.31 KB
/
MatrixMultiplication.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Program to multiply two given matrices.
import java.util.Scanner;
public class MatrixMultiplication
{
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
System.out.print("\nEnter the row size of first matrix, row1: ");
int row1 = reader.nextInt();
System.out.print("\nEnter the column size of first matrix, col1: ");
int col1 = reader.nextInt();
System.out.print("\nEnter the row size of second matrix, row2: ");
int row2 = reader.nextInt();
System.out.print("\nEnter the column size of second matrix, col2: ");
int col2 = reader.nextInt();
if(col1 == row2)
{
//code to input first matrix
int matrix1[][] = new int[row1][col1];
System.out.print("\nEnter the first matix -----------");
for(int i=0; i<row1; i++)
{
for(int j=0; j<col1 ; j++)
{
System.out.print("\nEnter the "+(i+1)+(j+1)+" element: ");
matrix1[i][j] = reader.nextInt();
}
}
// code to input second matrix
int matrix2[][] = new int[row2][col2];
System.out.print("\nEnter the second matix -----------");
for(int i=0; i<row2; i++)
{
for(int j=0; j<col2 ; j++)
{
System.out.print("\nEnter the "+(i+1)+(j+1)+" element: ");
matrix2[i][j] = reader.nextInt();
}
}
//code to perform matrix multilpication
int[][] matrix3 = new int[row1][col2];
for(int i=0; i<row1; i++)
{
for(int k=0;k<col2;k++)
{
int temp = 0 ;
for(int j=0;j<col1; j++)
{
temp = temp + matrix1[i][j]*matrix2[j][k];
}
matrix3[i][k] = temp;
}
}
// code to diplay the matrix and their output
System.out.print("\nEntered First Matrix: \n");
for(int i=0; i<row1; i++)
{
System.out.print("\n");
for(int j=0;j<col1;j++)
{
System.out.print("\t" + matrix1[i][j]);
}
}
System.out.print("\nEntered Second Matrix: \n");
for(int i=0; i<row2; i++)
{
System.out.print("\n");
for(int j=0;j<col2;j++)
{
System.out.print("\t" + matrix2[i][j]);
}
}
System.out.print("\nRequired multiplication is: \n");
for(int i=0; i<row1; i++)
{
System.out.print("\n");
for(int j=0;j<col2;j++)
{
System.out.print("\t" + matrix3[i][j]);
}
}
}
else
{
System.out.print("\nMultiplication is not possible as matrix dimensions are not consistent.");
}
}
}