1. Matrix printing without user interaction public class M1 { public static void main(String[] args) { int matrix[][] = {{1,2,3}, {4,5,6}, {7,8,9}}; int row = matrix.length; int col = matrix[0].length; System.out.println("\nMatrix Printing..."); for(int i=0; i<row;i++) { for(int j=0; j<col;j++) System.out.print(matrix[i][j]+" "); System.out.println(); } } } OUTPUT Matrix Printing... 1 2 3 4 5 6 7 8 9
2. Matrix printing Using Scanner (Taking input from user) import java.util.Scanner; public class M2 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("enter rows"); int m = s.nextInt(); System.out.println("enter coloumn"); int n = s.nextInt(); int mat[][] = new int[m][n]; System.out.println("enter elements.."); for(int i=0; i<m;i++){ for(int j=0; j<n;j++) mat[i][j]=s.nextInt(); } System.out.println("................"); for(int i=0; i<m;i++){ for(int j=0; j<n;j++) System.out.print(mat[i][j] +" "); System.out.println(); } } } OUTPUT enter rows 3 enter coloumn 3 enter elements.. 1 2 3 0 6 5 0 4 7 ................ 1 2 3 0 6 5 0 4 7
No comments:
Post a Comment
If you have any doubts, please let me know