Page 401 - CA_Blue( J )_Class10
P. 401
18 for (int j = 0; j < cols; j++) {
19 System.out.print("Element at [" + i + "][" + j + "]: ");
20 matrix[i][j] = scanner.nextInt();
21 }
22 }
23
24 // Displaying the elements
25 // of the two-dimensional array in matrix format
26 System.out.println("The matrix is:");
27 for (int i = 0; i < rows; i++) {
28 for (int j = 0; j < cols; j++) {
29 System.out.print(matrix[i][j] + "\t");
30 }
31 System.out.println(); // Move to the next line after each row
32 }
33
34 // Close the scanner
35 scanner.close();
36 }
37 }
You will get the following output:
Enter the number of rows: 2
Enter the number of columns: 2
Enter the elements of the matrix:
Element at [0][0]: 1
Element at [0][1]: 2
Element at [1][0]: 3
Element at [1][1]: 4
The matrix is:
1 2
3 4
399
Arrays 399

