FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/07-Java-Programs/070-Two-Dimensional-Array.java at main · shaikbasha-dev/Java · GitHub
shaikbasha-dev
/
Java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
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
Java
/
07-Java-Programs
/
070-Two-Dimensional-Array.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
103 lines (75 loc) · 3.06 KB
Breadcrumbs
Java
/
07-Java-Programs
/
070-Two-Dimensional-Array.java
Copy path
File metadata and controls
103 lines (75 loc) · 3.06 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
/*
* ============================================================================
* Program Name : Two-Dimensional Array
* File Name : 070-Two-Dimensional-Array.java
* Class Name : TwoDimensionalArray
*
* Description:
* This program accepts the number of rows, columns, and elements of a
* two-dimensional array from the user and displays the array in matrix form.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to declare and initialize a two-dimensional array.
* - Learn how to access and display matrix elements using nested loops.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import
java
.
util
.
Scanner
;
public
class
TwoDimensionalArray
{
// 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 a two-dimensional integer array.
int
[][]
array
=
new
int
[
rows
][
columns
];
// Ask the user to enter the array elements.
System
.
out
.
println
(
"Enter the Array Elements:"
);
// Read all array elements using nested loops.
for
(
int
i
=
0
;
i
<
rows
;
i
++) {
for
(
int
j
=
0
;
j
<
columns
;
j
++) {
// Store the current element in the array.
array
[
i
][
j
] =
scanner
.
nextInt
();
}
}
// Display the array elements.
System
.
out
.
println
(
"Two-Dimensional Array:"
);
// Traverse the array using nested loops.
for
(
int
i
=
0
;
i
<
rows
;
i
++) {
for
(
int
j
=
0
;
j
<
columns
;
j
++) {
// Print the current array element.
System
.
out
.
print
(
array
[
i
][
j
] +
" "
);
}
// Move to the next row.
System
.
out
.
println
();
}
// Example Output:
// Enter the Number of Rows: 2
// Enter the Number of Columns: 3
// Enter the Array Elements:
// 10 20 30
// 40 50 60
// Two-Dimensional Array:
// 10 20 30
// 40 50 60
}
// Close the Scanner object to release system resources.
scanner
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL