Java Program to Print Matrix in Z form Last Updated : 04 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9 Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 Java // Java program to print a // square matrix in Z form import java.lang.*; import java.io.*; class GFG { public static void diag(int arr[][], int n) { int i = 0, j, k; // print first row for (j = 0; j < n - 1; j++) { System.out.print(arr[i][j] + " "); } // Print diagonal k = 1; for (i = 0; i < n - 1; i++) { for (j = 0; j < n; j++) { if (j == n - k) { System.out.print(arr[i][j] + " "); break; } } k++; } // Print last row i = n - 1; for (j = 0; j < n; j++) System.out.print(arr[i][j] + " "); System.out.print("\n"); } public static void main(String[] args) { int a[][] = { { 4, 5, 6, 8 }, { 1, 2, 3, 1 }, { 7, 8, 9, 4 }, { 1, 8, 7, 5 } }; diag(a, 4); } } // Code contributed by Mohit Gupta_OMG <(0_o)> Output:4 5 6 8 3 8 1 8 7 5 Time Complexity: O(n*n)Auxiliary Space: O(1) Please refer complete article on Program to Print Matrix in Z form for more details! Comment More infoAdvertise with us Next Article Java Multi-Dimensional Arrays K kartik Follow Improve Article Tags : Java Practice Tags : Java Similar Reads How to read a Matrix from user in Java? Given task is to read a matrix from the user. The size and number of elements of matrices are to be read from the keyboard. Java // Java program to read a matrix from user import java.util.Scanner; public class MatrixFromUser { // Function to read matrix public static void readMatrixByUser() { int m 2 min read Simplest and Best method to print a 2D Array in Java Given a 2d array arr in Java, the task is to print the contents of this 2d array. Method 1: Loop method The first thing that comes to mind is to write a nested for loop, and print each element by arr[i][j]. Java // Java program to print 2d array // using Loop method import java.io.*; import java.uti 2 min read Java Arrays Coding Practice Problems Arrays are a fundamental data structure in Java programming, enabling efficient storage, manipulation, and retrieval of elements. This collection of Java array practice problems covers essential operations, including array traversal, sorting, searching, matrix manipulations, and element-wise calcula 2 min read Different Ways To Declare And Initialize 2-D Array in Java An array with more than one dimension is known as a multi-dimensional array. The most commonly used multi-dimensional arrays are 2-D and 3-D arrays. We can say that any higher dimensional array is an array of arrays. A very common example of a 2D Array is Chess Board. A chessboard is a grid containi 5 min read Java Multi-Dimensional Arrays Multidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. T 10 min read Sorting a 2D Array according to values in any given column in Java We are given a 2D array of order N X M and a column number K ( 1<=K<=m). Our task is to sort the 2D array according to values in Column K.Examples:Input: If our 2D array is given as (Order 4X4) 39 27 11 42 10 93 91 90 54 78 56 89 24 64 20 65Sorting it by values in column 3 Output: 39 27 11 422 3 min read Like