FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
01-Core-Java/08-Java-Programs/071-Matrix-Addition.java at main · shaikbasha-dev/01-Core-Java · GitHub
shaikbasha-dev
/
01-Core-Java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
01-Core-Java
/
08-Java-Programs
/
071-Matrix-Addition.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
172 lines (117 loc) · 4.6 KB
Breadcrumbs
01-Core-Java
/
08-Java-Programs
/
071-Matrix-Addition.java
Copy path
File metadata and controls
172 lines (117 loc) · 4.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 Addition
* File Name : 071-Matrix-Addition.java
* Class Name : MatrixAddition
*
* Description:
* This program accepts two matrices of the same order from the user
* and performs matrix addition. 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 addition using nested loops.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import
java
.
util
.
Scanner
;
public
class
MatrixAddition
{
// 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 addition.
for
(
int
i
=
0
;
i
<
rows
;
i
++) {
for
(
int
j
=
0
;
j
<
columns
;
j
++) {
// Add 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 Addition:"
);
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:
// 1 2
// 3 4
// Enter the Elements of Second Matrix:
// 5 6
// 7 8
// First Matrix:
// 1 2
// 3 4
// Second Matrix:
// 5 6
// 7 8
// Resultant Matrix After Addition:
// 6 8
// 10 12
}
// Close the Scanner object to release system resources.
scanner
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL