Java Program to Display Upper Triangular Matrix

Upper Triangular Matrix is a matrix in which all the elements below the principal matrix are 0. A necessary condition is that matrix must be Square matrix in nature. If the matrix is not a square matrix, it can never be called the upper triangular matrix.

Examples

Input 1: mat[][] = { {2, 1, 4},
                     {1, 2, 3},  
                     {3, 6, 2} }
                     
Output :  mat[][]= { {2, 1, 4},
                     {0, 2, 3},  
                     {0, 0, 2} }
                     
Explaination: All the element below the principal diagonal needs to be 0.

Input 2 :   mat[][]=  { {4,7},
                        {2,8} }
                  
Output :   mat[][]=   { {4,7},
                        {0,8} }

Input 3 :  mat[][]=  { {2,1,4,6},
                       {1,2,3,7},  
                       {3,6,2,8} }  
Output :   Matrix should be a Square Matrix

 Approach:

  • If the matrix has equal rows and columns, continue the program else exit the program.
  • Run the loop over the whole matrix, and for the rows whose row number is greater than column number, make the element at that position equal to 0.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to print the upper triangular matrix
import java.io.*;
  
class GFG {
    
    // Print matrix using iterative approach
    public static void printMatrix(int[][] a)
    {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[0].length; j++)
                System.out.print(a[i][j] + " ");
            System.out.println();
        }
    }
    public static void upperTriangularMatrix(int matrix[][])
    {
        int row = matrix.length;
        int col = matrix[0].length;
        
        // if number of rows and 
        // columns are not equal,then
        // return back
        if (row != col) {
            System.out.println(
                "Matrix should be a Square Matrix");
            return;
        }
        else {
            
            // looping over the whole matrix
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    
                    // for the rows ,whose row number is
                    // greater then column number,mark the
                    // element as 0
                    if (i > j) {
                        matrix[i][j] = 0;
                    }
                }
            }
  
            System.out.println(
                "Upper Traingular Matrix is given by :-");
            
            // printing the upper triangular matrix
            printMatrix(matrix);
        }
    }
    public static void main(String[] args)
    {
        int mat[][] = { { 2, 1, 4 }, { 1, 2, 3 }, { 3, 6, 2 } };
        
        // calling the function
        upperTriangularMatrix(mat);
    }
}

chevron_right


Output

Upper Traingular Matrix is given by :-
2 1 4 
0 2 3 
0 0 2 

Space Complexity: 0(N^2)

Time Complexity: 0(N^2)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.