-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path072-Matrix-Subtraction.java
More file actions
172 lines (117 loc) · 4.63 KB
/
Copy path072-Matrix-Subtraction.java
File metadata and controls
172 lines (117 loc) · 4.63 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* ============================================================================
* Program Name : Matrix Subtraction
* File Name : 072-Matrix-Subtraction.java
* Class Name : MatrixSubtraction
*
* Description:
* This program accepts two matrices of the same order from the user
* and performs matrix subtraction. The resulting matrix is then displayed.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to declare and use two-dimensional arrays.
* - Perform matrix subtraction using nested loops.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import java.util.Scanner;
public class MatrixSubtraction {
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Create a Scanner object to read input from the keyboard.
Scanner scanner = new Scanner(System.in);
// Ask the user to enter the number of rows.
System.out.print("Enter the Number of Rows: ");
// Read the number of rows.
int rows = scanner.nextInt();
// Ask the user to enter the number of columns.
System.out.print("Enter the Number of Columns: ");
// Read the number of columns.
int columns = scanner.nextInt();
// Check whether the entered dimensions are valid.
if (rows <= 0 || columns <= 0) {
// Display an error message.
System.out.println("Please enter positive values for rows and columns.");
} else {
// Declare the first matrix.
int[][] matrix1 = new int[rows][columns];
// Declare the second matrix.
int[][] matrix2 = new int[rows][columns];
// Declare the result matrix.
int[][] result = new int[rows][columns];
// Ask the user to enter the elements of the first matrix.
System.out.println("Enter the Elements of First Matrix:");
// Read the first matrix elements.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
// Store the current element.
matrix1[i][j] = scanner.nextInt();
}
}
// Ask the user to enter the elements of the second matrix.
System.out.println("Enter the Elements of Second Matrix:");
// Read the second matrix elements.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
// Store the current element.
matrix2[i][j] = scanner.nextInt();
}
}
// Perform matrix subtraction.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
// Subtract the corresponding elements.
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
// Display the first matrix.
System.out.println("First Matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix1[i][j] + " ");
}
System.out.println();
}
// Display the second matrix.
System.out.println("Second Matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix2[i][j] + " ");
}
System.out.println();
}
// Display the resultant matrix.
System.out.println("Resultant Matrix After Subtraction:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
// Example Output:
// Enter the Number of Rows: 2
// Enter the Number of Columns: 2
// Enter the Elements of First Matrix:
// 8 6
// 5 4
// Enter the Elements of Second Matrix:
// 3 2
// 1 4
// First Matrix:
// 8 6
// 5 4
// Second Matrix:
// 3 2
// 1 4
// Resultant Matrix After Subtraction:
// 5 4
// 4 0
}
// Close the Scanner object to release system resources.
scanner.close();
}
}