You are given a 3x3 matrix with numbers. Output the numbers of the array, each on a new line. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

You are given a 3x3 matrix with numbers. Output the numbers of the array, each on a new line.

public class Main { public static void main(String[] args) { int[][] matrix = { {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, }; //output the numbers System.out.println(matrix[0][0]); System.out.println(matrix[0][1]); System.out.println(matrix[0][2]); System.out.println(matrix[1][0]); System.out.println(matrix[1][1]); System.out.println(matrix[1][2]); System.out.println(matrix[2][0]); System.out.println(matrix[2][1]); System.out.println(matrix[2][2]);} } I could make it this way. How it can be done by nested 2 for loops

8th May 2021, 8:06 AM
Swapnil Kamdi
Swapnil Kamdi - avatar
4 Answers
+ 5
for(int i =0;i<3;i++) for(int j = 0;j<3;j++) System.out.println(matrix[i][j]);
8th May 2021, 8:23 AM
TOLUENE
TOLUENE - avatar
+ 2
public class Main { public static void main(String[] args) { int[][] matrix = { {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, {4, 9, 2} }; //output the numbers // returns the length of the rows in the array int lengthOne = matrix.length; System.out.println("rwo "+lengthOne); int lengthTwo = matrix[0].length; System.out.println("col "+lengthTwo); for (int x=0; x < lengthOne; x++) { for (int y=0; y < lengthTwo ; y++) { System.out.println(matrix[x][y]); } } } }
5th Jul 2021, 1:30 AM
Gowri Kumar
Gowri Kumar - avatar
+ 2
public class Main { public static void main(String[] args) { int[][] matrix = { {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, }; //output the numbers for (int i=0;i<matrix.length;i++){ for(int j=0;j<matrix.length;j++){ System.out.println(matrix[i][j]); } } } }
31st Dec 2021, 2:52 PM
Shuvajit Ghosh
0
public class Main { public static void main(String[] args) { int[][] matrix = { {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, }; //output the numbers for (int i=0;i<matrix.length;i++){ for(int j=0;j<matrix.length;j++){ System.out.println(matrix[i][j]); } } } } Good Luck
26th Jan 2022, 6:28 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar