forked from vishnu2k60/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubtractMatrices.java
42 lines (36 loc) · 1.25 KB
/
SubtractMatrices.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
public class Sub_Matrix
{
public static void main(String[] args) {
int rows, cols;
//Initialize matrix a
int a[][] = {
{4, 5, 6},
{3, 4, 1},
{1, 2, 3}
};
//Initialize matrix b
int b[][] = {
{2, 0, 3},
{2, 3, 1},
{1, 1, 1}
};
//Calculates number of rows and columns present in given matrix
rows = a.length;
cols = a[0].length;
//Array diff will hold the result
int diff[][] = new int[rows][cols];
//Performs subtraction of matrices a and b. Store the result in matrix diff
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
diff[i][j] = a[i][j] - b[i][j];
}
}
System.out.println("Subtraction of two matrices: ");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.print(diff[i][j] + " ");
}
System.out.println();
}
}
}