Java Program to display upper triangular matrix


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

Below is a demonstration of the same −

Suppose our input is

The matrix is defined as:
2 1 4
1 2 3
3 6 2

The desired output would be

The upper triangular matrix is:
2 1 4
0 2 3
0 0 2

Algorithm

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix.
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using two for-loops, assign 0 to all the [i][j] positions that comes below the diagonal of the matrix using rows != column condition.
Step 5 - Display the matrix as result
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

public class UpperTriangle {
   public static void upper_triangular_matrix(int input_matrix[][]) {
   }
   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("\nThe 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();
         }
      }
   }
}

Output

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

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

public class UpperTriangle {
   public static void upper_triangular_matrix(int input_matrix[][]) {
      int rows = input_matrix.length;
      int column = input_matrix[0].length;
      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("\nThe 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();
         }
      }
   }
   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();
      }
      upper_triangular_matrix(input_matrix);
   }
}

Output

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

Updated on: 29-Mar-2022

584 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements