Program to Print Matrix in Z form
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

Complexity O(n)
We need to traverse first row of matrix then second diagonal and then last row.
CPP
// CPP program to print a square matrix in Z form #include <bits/stdc++.h> using namespace std; const int MAX = 100; // Function to print a square matrix in Z form void printZform(int mat[][MAX], int n) { // print first row for (int i = 0; i < n; i++) cout << mat[0][i] << " "; // Print diagonal int i = 1, j = n - 2; while (i < n && j >= 0) // print diagonal { cout << mat[i][j] << " "; i++; j--; } // Print last row for (int i = 1; i < n; i++) cout << mat[n - 1][i] << " "; } // Driver function int main() { int mat[][MAX] = { { 4, 5, 6, 8 }, { 1, 2, 3, 1 }, { 7, 8, 9, 4 }, { 1, 8, 7, 5 } }; printZform(mat, 4); return 0; } |
chevron_right
filter_none
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)> |
chevron_right
filter_none
Python3
# Python program to print a # square matrix in Z form arr = [[4, 5, 6, 8], [1, 2, 3, 1], [7, 8, 9, 4], [1, 8, 7, 5]] n = len(arr[0]) i = 0for j in range(0, n-1): print(arr[i][j], end = ' ') k = 1for i in range(0, n): for j in range(n, 0, -1): if(j == n-k): print(arr[i][j], end = ' ') break; k+= 1 # Print last row i = n-1; for j in range(0, n): print(arr[i][j], end = ' ') # Code contributed by Mohit Gupta_OMG <(0_o)> |
chevron_right
filter_none
C#
// C# program to print a square // matrix in Z form using System; class GFG { public static void printZform(int[, ] mat, int n) { int i, j; // print first row for (i = 0; i < n; i++) { Console.Write(mat[0, i] + " "); } // Print diagonal i = 1; j = n - 2; while (i < n && j >= 0) // print diagonal { Console.Write(mat[i,j] + " "); i++; j--; } // Print last row for (i = 1; i < n; i++) Console.Write(mat[n - 1,i] + " "); } // Driver code public static void Main() { int[, ] mat = { { 4, 5, 6, 8 }, { 1, 2, 3, 1 }, { 7, 8, 9, 4 }, { 1, 8, 7, 5 } }; printZform(mat, 4); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP program to print a // square matrix in Z form $MAX = 100; // Function to print a // square matrix in Z form function printZform( $mat, $n) { // print first row for($i = 0; $i < $n; $i++) echo $mat[0][$i] , " "; // Print diagonal $i = 1;$j = $n - 2; // print diagonal while ($i < $n and $j >= 0) { echo $mat[$i][$j] , " "; $i++; $j--; } // Print last row for ( $i = 1; $i < $n; $i++) echo $mat[$n - 1][$i] , " "; } // Driver Code $mat = array(array(4, 5, 6, 8), array(1, 2, 3, 1), array(7, 8, 9, 4), array(1, 8, 7, 5)); printZform($mat, 4); // This code is contributed by anuj_67. ?> |
chevron_right
filter_none
Output:
4 5 6 8 3 8 1 8 7 5
This article is contributed by R_Raj. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Print matrix in antispiral form
- Print a given matrix in spiral form
- Print a given matrix in zigzag form
- Print K'th element in spiral form of matrix
- Print a matrix in Reverse Wave Form
- Print a given matrix in reverse spiral form
- Print a matrix in a spiral form starting from a point
- Print a given matrix in spiral form using direction tracking method
- Print a given matrix in counter-clock wise spiral form
- Program to print numbers in digital form
- Program to print the Diagonals of a Matrix
- Program to print elements of a Matrix row-wise skipping alternate elements
- Program to print Lower triangular and Upper triangular matrix of an array
- Form coils in a matrix
- Form a Spiral Matrix from the given Array
Improved By : vt_m



