Skip to content

Commit 572037b

Browse files
committed
feat: Read and display a 2x2 matrix using nested loops
🧠 Logic: - Declare a 2x2 integer matrix. - Use nested `for` loops to input values from the user. - Use nested loops again to print the matrix in row-major order. - Demonstrates basic 2D array handling with Scanner input. Signed-off-by: Somesh diwan <[email protected]>
1 parent a29f8be commit 572037b

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
import java.util.Scanner;
22

3-
class A2D
4-
{
5-
public static void main(String[] args)
6-
{
3+
class TwoDArrayPractice {
4+
public static void main(String[] args) {
75
int [][] A = new int [2][2];
6+
//Declaring a 2x2 2D Array.
7+
88
Scanner sc = new Scanner(System.in);
99
System.out.println("Enter the elements: ");
10-
for(int i = 0; i<2; i++)
11-
{
12-
for(int j = 0; j<2; j++)
13-
{
14-
A[i][j] = nextInt[];
10+
11+
for(int i = 0; i<2; i++) {
12+
for(int j = 0; j<2; j++) {
13+
A[i][j] = sc.nextInt();
1514
}
1615
}
1716

1817
System.out.println("Array elements are: ");
19-
for(int i = 0; i<2; i++)
20-
{
21-
for (int j = 0; j<2; j++)
22-
{
18+
for(int i = 0; i<2; i++) {
19+
for (int j = 0; j<2; j++) {
2320
System.out.print(A[i][j]+" ");
2421
}
2522
}
26-
2723
System.out.println("");
2824
}
29-
}
25+
}

0 commit comments

Comments
 (0)