Java program to print an upper triangular matrix:
In this post, we will learn how to print an upper triangular matrix in Java. The program will use a given matrix to print the upper triangular matrix. Before writing the program, let me quickly explain to you what is a upper triangular matrix and how it looks like.
Upper triangular matrix:
A square matrix is called an upper triangular matrix if all the elements below the main diagonal axis are zero. It is also called a right triangular matrix.
For example, the following two are upper triangular matrices:
8 8 8
0 6 3
0 0 1
8 9 1 2
0 2 3 1
0 0 8 9
0 0 0 1
In the second example, 8 2 8 1 is the main diagonal axis. All of the elements below this axis are zero. So, it is an upper triangular matrix.
Algorithm to convert a matrix to an upper triangular matrix:
To convert a given matrix to an upper triangular matrix, we will use the below algorithm.
- Initialize the given matrix in a 2D array.
- Run one loop for row number of times. For example, i is used for the outer loop and j is used for the inner loop. On each iteration of the outer loop, run another inner loop for i number of times.
- In the inner loop, assign the value at [i][j] of the 2D array to 0. Else, exit from the inner loop.
- At the end of the program, the given matrix will be converted to an upper triangular matrix.
Example 1: Java program to convert a matrix to an upper triangular matrix:
Let’s write down the Java program to convert a matrix to an upper triangular matrix:
public class Main {
private static void printMatrix(int[][] m) {
int size = m.length;
for (int[] ints : m) {
for (int j = 0; j < size; j++) {
System.out.print(ints[j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] givenMatrix = {
{8, 9, 1, 2},
{1, 2, 3, 1},
{2, 4, 6, 5},
{3, 1, 5, 6}
};
System.out.println("Given Matrix: ");
printMatrix(givenMatrix);
for (int i = 0; i < givenMatrix.length; i++) {
for (int j = 0; j < i; j++) {
givenMatrix[i][j] = 0;
}
}
System.out.println("Upper triangular Matrix: ");
printMatrix(givenMatrix);
}
}
In this program,
- The printMatrix method is used to print a matrix. It takes one 2D array as its parameter and prints the content of it. It uses two for loops to print the values.
- In the main method, we initialized one 2D array called givenMatrix. It is a 4x4 matrix. It calls the printMatrix method to print the matrix.
- It uses two for loops to convert the matrix to an upper triangular matrix. The outer for loop runs for row number of times. On each iteration, the inner loop runs for i number of times. It assigns 0 to the [i][j] member of the matrix givenMatrix.
- The last line is printing the final upper triangular matrix.
If you run this program, it will print the below output:
Given Matrix:
8 9 1 2
1 2 3 1
2 4 6 5
3 1 5 6
Upper triangular Matrix:
8 9 1 2
0 2 3 1
0 0 6 5
0 0 0 6
Example 2: Java program to convert a matrix to an upper triangular matrix by using a separate method:
The above program is not using any different method to convert a matrix to upper triangular matrix. We can write a separate method and call it from the main method to do the conversion.
public class Main {
private static void printMatrix(int[][] m) {
int size = m.length;
for (int[] ints : m) {
for (int j = 0; j < size; j++) {
System.out.print(ints[j] + " ");
}
System.out.println();
}
}
private static void convertToUpperTriangular(int[][] m) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < i; j++) {
m[i][j] = 0;
}
}
}
public static void main(String[] args) {
int[][] givenMatrix = {
{8, 9, 1, 2},
{1, 2, 3, 1},
{2, 4, 6, 5},
{3, 1, 5, 6}
};
System.out.println("Given Matrix: ");
printMatrix(givenMatrix);
convertToUpperTriangular(givenMatrix);
System.out.println("Upper triangular Matrix: ");
printMatrix(givenMatrix);
}
}
This is similar to the above program. But, we created a new method called convertToUpperTriangular to convert the matrix to an upper triangular matrix. It takes the matrix as its parameter and converts it to an upper triangular. It is similar to the above program.
If you run this program, it will print the same output.
You might also like:
- Java program to check if a number is a composite number or not
- Java next() vs nextLine() differences with examples
- Java program to check for disjoint sets in two ways
- 4 different Java programs to convert a list to set
- Java program to convert an image to Base64 in 2 different ways
- Java program to print odd numbers from 1 to 100