
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Print Matrix in Z form
To print matrix in Z form, the Java code is as follows −
Example
import java.lang.*; import java.io.*; public class Demo{ public static void z_shape(int my_arr[][], int n){ int i = 0, j, k; for (j = 0; j < n - 1; j++){ System.out.print(my_arr[i][j] + " "); } k = 1; for (i = 0; i < n - 1; i++){ for (j = 0; j < n; j++){ if (j == n - k){ System.out.print(my_arr[i][j] + " "); break; } } k++; } i = n - 1; for (j = 0; j < n; j++) System.out.print(my_arr[i][j] + " "); System.out.print("\n"); } public static void main(String[] args){ int my_arr[][] = { { 34, 67, 89, 0},{ 0, 1,0, 1 },{ 56, 99, 102, 21 },{78, 61, 40, 99}}; System.out.println("The matrix is "); z_shape(my_arr, 4); } }
Output
The matrix is 34 67 89 0 0 99 78 61 40 99
A class named Demo defines a function named ‘z_shape’, that iterates through the array, by following the shape of ‘z’. In the main function, the multidimensional array is defined, and the function is called by passing this array. The relevant output is displayed on the console.
- Related Articles
- Python Program to Print Matrix in Z form
- C++ Program to Print Matrix in Z form?
- Swift Program to Print Matrix numbers containing in Z form
- Program to Print the Squared Matrix in Z form in C
- Java program to print a given matrix in Spiral Form.
- Print matrix in antispiral form
- Java program to print the transpose of a matrix
- Java Program to Print Boundary Elements of a Matrix
- Java Program to Print the Multiplication Table in Triangular Form
- Print a matrix in Reverse Wave Form in C++
- Print a given matrix in zigzag form in C++
- Program to print a matrix in Diagonal Pattern.
- Print a given matrix in reverse spiral form in C++
- Python Program to Print an Identity Matrix
- Golang Program to Print an Identity Matrix

Advertisements