In the previous article, we have seen Java Program to Decrement the Matrix Elements by a Specified Number
In this article we are going to see how we can print matrix in Z form in JAVA language.
Java Program to Print Matrix in Z form
A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.
Let’s understand it in more simpler way.
| A00 A01 A02 | Matrix A = | A10 A11 A12 | | A20 A21 A22 | 3*3
Matrix A
represents a 3*3 matrix.- ‘
A
‘ represents the matrix element - ‘
Aij
‘ represents the matrix element at it’s matrix position/index. - ‘
i
‘ represents the row index - ‘
j
‘ represents the column index - Means
A00=Aij
wherei=0
andj=0
,A01=aij
wherei=0
andj=1
and like this. - Here we have started
row
value from 0 andcolumn
value from 0.
Let’s see different ways to print Matrix in Z form.
Method-1: Java Program to Print Matrix in Z form By Static Initialization of Array Elements
Approach:
- Initialize an array of size 3×3 with values.
- Show the array to the user.
- Print the first row
- Print the second diagonal excluding the first and last row.
- Print the last row
Program:
import java.util.Scanner; public class matrix { public static void main(String args[]) { //Scanner class to take input Scanner scan = new Scanner(System.in); // Initializing the 3X3 matrix i.e. 2D array int arr[][] = {{1,0,0},{0,0,0},{1,1,1}}; int row, col ; System.out.print("The matrix elements are:"); printMatrix(arr); System.out.println("\nThe z-form :"); printZMatrix(arr); } // Function to print the matrix static void printMatrix(int arr[][]) { int row, col; // Loop to print the elements for(row=0;row<3;row++) { // Used for formatting System.out.print("\n"); for(col=0;col<3;col++) { System.out.print(arr[row][col]+" "); } } } // Looks for the highet occurence of 1 static void printZMatrix(int arr[][]) { int row, col; // Print the first row for(col=0;col<3;col++) System.out.print(arr[0][col]+" "); // Used for formatting System.out.print("\n"); // Print the second diagonal(Here Mid element of 2nd row) for(row=1;row<2;row++) for(col=0;col<3;col++) if(row+col==2) System.out.print(" "+arr[row][col]); // Used for formatting System.out.print("\n"); // Print the last row for(col=0;col<3;col++) System.out.print(arr[2][col]+" "); } }
Output: The matrix elements are: 1 0 0 0 0 0 1 1 1 The z-form : 1 0 0 0 1 1 1
Method-2: Java Program to Print Matrix in Z form By Dynamic Initialization of Array Elements
Approach:
- Declare an array of size 3×3.
- Ask the user for input of array elements.
- Use two for loops to iterate the rows and columns to input the array elements.
- Print the first row.
- Print the second diagonal excluding the first and last row.
- Print the last row.
Program:
import java.util.Scanner; public class matrix{ public static void main(String args[]) { //Scanner class to take input Scanner scan = new Scanner(System.in); // Initializing the 3X3 matrix i.e. 2D array int arr[][] = new int[3][3]; System.out.println("Enter the 3x3 matrix elements :"); int row, col; // Loop to take user input for(row=0;row<3;row++) for(col=0;col<3;col++) arr[row][col] = scan.nextInt(); System.out.print("The matrix elements are:"); printMatrix(arr); System.out.println("\nThe z-form :"); printZMatrix(arr); } // Function to print the matrix static void printMatrix(int arr[][]) { int row, col; // Loop to print the elements for(row=0;row<3;row++) { // Used for formatting System.out.print("\n"); for(col=0;col<3;col++) { System.out.print(arr[row][col]+" "); } } } // Looks for the highet occurence of 1 static void printZMatrix(int arr[][]) { int row, col; // Print the first row for(col=0;col<3;col++) System.out.print(arr[0][col]+" "); // Used for formatting System.out.print("\n"); // Print the second diagonal for(row=1;row<2;row++) for(col=0;col<3;col++) if(row+col==2) System.out.print(" "+arr[row][col]); // Used for formatting System.out.print("\n"); // Print the last row for(col=0;col<3;col++) System.out.print(arr[2][col]+" "); } }
Output: Enter the 3x3 matrix elements : 1 1 1 1 1 1 1 1 1 1 The matrix elements are: 1 1 1 1 1 1 1 1 1 The z-form : 1 1 1 1 1 1 1
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Related Java Programs: