×

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

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Session Tracking in Java

When a series of requests from the same User (i.e., requests coming from the same browser) occurs over an extended period of time, servlets employ a mechanism known as session...

3 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.