Display Upper Triangular Matrix in Java



In this article, we will understand how to display an upper triangular matrix of a given matrix in Java. A matrix has row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix. And, the term upper triangular matrix refers to a matrix whose all elements below the main diagonal are 0.

Example Scenario:

Input: matrix = { 2, 1, 4 }, { 1, 2, 3 }, { 3, 6, 2 } Output: upptri_mat = 2 1 4 0 2 3 0 0 2

Using Nested for Loop

To display upper triangular matrix of a given matrix, iterate over each element of the matrix using nested for loop. Then, assign 0 to all the [i][j] positions that comes below the diagonal of the matrix using rows != column condition.

Example

In the following Java program, we use the nested for loop to print the upper triangular matrix.

Open Compiler
public class UpperTriangle { public static void main(String[] args) { int input_matrix[][] = { { 2, 1, 4 }, { 1, 2, 3 }, { 3, 6, 2 } }; int rows = input_matrix.length; int column = input_matrix[0].length; System.out.println("The matrix is defined as: "); for (int i = 0; i < rows; i++) { for (int j = 0; j < column; j++) { System.out.print(input_matrix[i][j] + " "); } System.out.println(); } if (rows != column) { return; } else { for (int i = 0; i < rows; i++) { for (int j = 0; j < column; j++) { if (i > j) { input_matrix[i][j] = 0; } } } System.out.println("The upper triangular matrix is: "); for (int i = 0; i < rows; i++) { for (int j = 0; j < column; j++) { System.out.print(input_matrix[i][j] + " "); } System.out.println(); } } } }

On running this code, you will get the following result ?

The matrix is defined as:
2 1 4
1 2 3
3 6 2
The upper triangular matrix is:
2 1 4
0 2 3
0 0 2

Using Java Streams

In this approach, we use nested IntStream.range() method instead of for loops to iterate over each element of the matrix. For each element, if its row index is greater than its column index (i.e., below the main diagonal), print "0". Otherwise, print the element's value.

Example

In this Java program, we use the Java streams to display upper triangular matrix.

Open Compiler
import java.util.Arrays; import java.util.stream.IntStream; public class UpperTriangle { public static void main(String[] args) { int[][] input_matrix = { { 2, 3, 6 }, { 1, 2, 2 }, { 1, 4, 2 } }; int rows = input_matrix.length; int column = input_matrix[0].length; System.out.println("Given Matrix: "); for (int i = 0; i < rows; i++) { for (int j = 0; j < column; j++) { System.out.print(input_matrix[i][j] + " "); } System.out.println(); } System.out.println("The upper triangular matrix is: "); IntStream.range(0, input_matrix.length).forEach(i -> { IntStream.range(0, input_matrix[i].length) .mapToObj(j -> j < i ? "0" : String.valueOf(input_matrix[i][j])) .forEach(n -> System.out.print(n + " ")); System.out.println(); }); } }

Output of the code is as follows ?

Given Matrix: 
2 3 6 
1 2 2 
1 4 2 
The upper triangular matrix is: 
2 3 6 
0 2 2 
0 0 2 
Updated on: 2024-09-30T16:07:07+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements