×

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form.

What is a Matrix in Java?

Technically referring, a matrix is a rectangular, two-dimensional array that contains either integers, symbols, or statements as its fundamental elements. The elements in the matrix are arranged in rows and columns.

For an instance, m x n matrix refers to a matrix containing 'm' number of rows and 'n' number of columns. 'a ij', which denotes that the element an is represented in the ith row and jth column, can be used to represent individual values in the matrix, which are referred to as elements.

Look at the example below to understand how a matrix actually looks like

Example:

                              a[][]= { 7, 8, 9,

                                    4, 5, 6,

                                    1, 2, 3}

The above example is a matrix or it can also be termed as two-dimensional array, with 3 rows and 3 columns. The representation of the above matrix elements in Z form shall certainly include the entire first row of the matrix, the right diagonal and the entire last row of the matrix too.

Now let us discuss how to print the elements of a matrix in Z from.

The very first row, right diagonal, and then last row of the matrix are all represented in the Z form. The columns and the rows must match for the matrix to be presented in its Z form.

We must print the matrix's components in Z form given a two - dimensional array of order n*n.

Input: 

            { [1, 2, 3,]

               [4, 5, 6]

               [7, 8, 9]}

Output:

             {  [        3]

                 [   5    ]

                 [7       ] }

Input:

           {[1, 2, 3, 4,]

             [5, 6, 7, 8,]

             [9, 8, 7, 6,]

             [5, 4, 3, 2,] }

Output:

           {[                4,]

            [           7,     ]

            [       8,         ]

            [5,                ] }

The above is how the matrix can be represented in Z form.

Now, let us look at the flow of program that prints the elements of a given matrix in Z form.

File name: Zform1.java

import java.util.Scanner;  
public class Zform1  
{  
//displaying the matrix 
static void printMatrix(int arr[][])  
{  
int row, col;   
for(row=0; row<3;row++)  
{  
System.out.print("\n"); 
for(col=0; col<3;col++)  
{  
//displaying the elements       
System.out.print(arr[row][col]+"\t");  
}  
}  
}    
static void printZMatrix(int arr[][])  
{  
int row, col;  
//displays the first row (first elements of all the columns) of matrix  
for(col=0;col<3;col++)  
System.out.print(arr[0][col]+"\t");  
System.out.print("\n");          
//displays the elements present in the right diagonal of the given matrix
//row begins with indexing 1 since we must not to display the starting and ending //element  
for(row=1; row<2; row++)  
for(col=0;col<3;col++)  
//verifies if the sum of rows and columns is equal to 2  
//If true it dislplays the first row and first column element  
if(row+col==2)  
System.out.print("\t"+arr[row][col]);  
System.out.print("\n");  
//displays the final row of matrix.
for(col=0;col<3;col++)  
System.out.print(arr[2][col]+"\t");  
}  
public static void main(String args[])  
{  
//creating a matrix of 3*3   
int arr[][] = {{8,7,6},{1,2,3},{6,9,1}};  
System.out.print("The elements in the matrix are:");  
printMatrix(arr);  
System.out.println("\n The elements of matrix in Z-form: ");  
//function calling  
printZMatrix(arr);  
}  
}   

Output

The elements in the matrix are:
8       7       6
1       2       3
6       9       1
The elements of matrix in Z-form are:
8       7       6
        2
6       9       1

Let’s go through another example

File name: Zform2.java

// Java program to display the elements in matrix in Z form
public final class Zform2 {
public static int MAX = 100;


// Function to print a square matrix in Z form
public static void printZform(int[][] mat, int n)
{
int i;


// Displays all elements in first row except the last one
for (i = 0; i < n - 1; i++) {
System.out.print(mat[0][i]);
System.out.print(" ");
}


// Displays the right diagonal from the given matrix
for (i = 0; i < n - 1; i++) {
System.out.print(mat[i][n - i - 1]);
System.out.print(" ");
}


// Displays the last row of the matrix
for (i = 0; i < n; i++) {
System.out.print(mat[n - 1][i]);
System.out.print(" ");
}
}
public static void main(String[] args)
{
int[][] m = { { 11,12,13,14},
        { 23,24,25,26},
        { 33,34,35,36},
        { 47,48,46,49} };
printZform(m, 4);
}
}

Output:

11 12 13 14 25 34 47 48 46 49

Related Topics

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

The final Keyword in Java

The final keyword is employed in several instances. Firstly, the non-access modifier final only applies to variables, methods, and classes. The final can be used in the following situations. Final Variables When...

6 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

4 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.