Skip to content

Commit a9990cf

Browse files
committed
feat: Accept and print a 2x2 matrix using nested loops
🧠 Logic: - Create a 2x2 integer matrix. - Accept 4 user inputs to fill the matrix using nested `for` loops. - Print each element twice (as per current logic) in a row-wise manner. - Each row ends with a newline for better formatting. Signed-off-by: Somesh diwan <[email protected]>
1 parent 41d9114 commit a9990cf

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import java.util.Scanner;
2-
class Array2D
3-
{
4-
public static void main(String [] args)
5-
{
2+
class Array2D {
3+
public static void main(String [] args) {
64
int [][] A=new int [2][2];
75
Scanner sc = new Scanner(System.in);
86
System.out.println("enter 4 elements");
9-
for(int i=0; i<2; i++)
10-
{
11-
for(int j=0; j<2; j++)
12-
{
7+
8+
for(int i=0; i<2; i++) {
9+
for(int j=0; j<2; j++) {
1310
A[i][j] = sc.nextInt();
1411
}
1512
}
1613
System.out.println("Array elements are");
17-
for(int i=0; i<2; i++)
18-
{
19-
for(int j=0; j<2; j++)
20-
{
21-
System.out.println(A[i][j] + " "+ A[i][j]);
14+
15+
for(int i=0; i<2; i++) {
16+
for(int j=0; j<2; j++) {
17+
System.out.print(A[i][j] + " ");
2218
}
2319
System.out.println(" ");
2420
}
2521
}
26-
}
22+
}

0 commit comments

Comments
 (0)