Java Program to display the upper triangular matrix

In this program, we need to display the upper triangular matrix.

Upper Triangular Matrix

Upper triangular matrix is a square matrix in which all the elements below the principle diagonal are zero. To find the upper triangular matrix, a matrix needs to be a square matrix that is, the number of rows and columns in the matrix need to be equal. Dimensions of a typical square matrix can be represented by n x n.

Java Program to display the upper triangular matrix

Consider the above example, principle diagonal element of given matrix is (1, 6, 6). All the elements below diagonal needs to be zero to convert it into an upper triangular matrix, in our example, those elements are at positions (2, 1), (3, 1) and (3, 2). To convert given matrix into the upper triangular matrix, loop through the matrix and set the values of the element to zero where row number is greater than column number.

Algorithm

  • STEP 1: START
  • STEP 2: DEFINE rows, cols
  • STEP 3: INITIALIZE matrix a[][] ={{1,2,3},{8, 6, 4}, {4, 5, 6}}
  • STEP 4: rows = a.length
  • STEP 5: cols = a[0].length
  • STEP 6: if(rows!=cols)
            then
            PRINT “Matrix should be a square matrix”
            else
            Go to step 7
  • STEP 7: REPEAT STEP 8 to STEP 10 UNTIL i<rows
            //for(i=0; i<rows; i++)
  • STEP 8: REPEAT STEP 9 UNTIL j<cols // for(j=0; j<cols; j++)
  • STEP 9: If(i>j) then PRINT 0 else PRINT a[i][j]
  • STEP 10: PRINT new line
  • STEP 11: END

program

  1. public class UpperTriangular    
  2. {    
  3.     public static void main(String[] args) {    
  4.         int rows, cols;    
  5.             
  6.         //Initialize matrix a    
  7.         int a[][] = {       
  8.                         {1, 2, 3},    
  9.                         {8, 6, 4},    
  10.                         {4, 5, 6}    
  11.                     };    
  12.               
  13.           //Calculates number of rows and columns present in given matrix    
  14.           rows = a.length;    
  15.         cols = a[0].length;    
  16.             
  17.         if(rows != cols){    
  18.             System.out.println(“Matrix should be a square matrix”);    
  19.         }    
  20.         else {    
  21.             //Performs required operation to convert given matrix into upper triangular matrix    
  22.             System.out.println(“Upper triangular matrix: “);    
  23.             for(int i = 0; i < rows; i++){    
  24.                 for(int j = 0; j < cols; j++){    
  25.                   if(i > j)    
  26.                     System.out.print(“0 “);    
  27.                   else    
  28.                     System.out.print(a[i][j] + ” “);    
  29.                 }    
  30.                 System.out.println();    
  31.             }    
  32.         }    
  33.     }    
  34. }    

Output:

Upper triangular matrix:
1	2   3
0    6   4
0    0   0

Leave a Comment