Java Program to Display Upper Triangular Matrix Last Updated : 23 Apr, 2023 Comments Improve Suggest changes Like Article Like Report 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. ExamplesInput 1: mat[][] = { {2, 1, 4}, {1, 2, 3}, {3, 6, 2} } Output : mat[][]= { {2, 1, 4}, {0, 2, 3}, {0, 0, 2} } Explanation: 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 // 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 Triangular 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); } } OutputUpper Triangular Matrix is given by :- 2 1 4 0 2 3 0 0 2 Space Complexity: 0(N^2) Time Complexity: 0(N^2) Comment More infoAdvertise with us Next Article Java Program to Display Upper Triangular Matrix L lavishgarg26 Follow Improve Article Tags : Java Java Programs DSA Practice Tags : Java Similar Reads Java Program to Display Lower Triangular Matrix Lower Triangular Matrix is a square matrix in which all the elements above the principal diagonal are 0. If the matrix is not a square matrix, it can never be called the lower triangular matrix. Examples Input 1: mat[][] = { {2, 1, 4}, {1, 2, 3}, {3, 6, 2}} Output : mat[][]= {{2, 0, 0}, {1, 2, 0}, { 2 min read Java Program to check if matrix is upper triangular Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}}; Output : Matrix is in 2 min read Java Program to Display Floydâs Triangle Floydâs triangle is a triangle with first natural numbers. It is the right arrangement of the numbers/values or patterns. Basically, it is a left to right arrangement of natural numbers in a right-angled triangle Illustration: Suppose if no of rows to be displayed is 5 then the desired output should 2 min read Java Program to check if matrix is lower triangular Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 0, 0, 0}, {1, 4, 0, 0}, {4, 6, 2, 0}, {0, 4, 7, 6}}; Output : Matrix is in 2 min read Java Program to Print Upper Star Triangle Pattern The upper star triangle pattern means the base has to be at the bottom and there will only be one star to be printed in the first row. Here is the issue that will arise is what way we traverse be it forward or backward we can't ignore the white spaces, so we are not able to print a star in the first 2 min read Java Program to Print Mirror Upper Star Triangle Pattern The pattern has two parts - both mirror reflections of each other. The base of the triangle has to be at the bottom of the imaginary mirror and the tip at the top. Illustration: Input: size = 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 5 min read Java Program to Print Downward Triangle Star Pattern Downward triangle star pattern means we want to print a triangle that is downward facing means base is upwards and by default, orientation is leftwards so the desired triangle to be printed should look like * * * * * * * * * * Example Java // Java Program to Print Lower Star Triangle Pattern // Main 2 min read Java Program to Add two Matrices Given two matrices A and B of the same size, the task is to add them in Java. Examples: Input: A[][] = {{1, 2}, {3, 4}} B[][] = {{1, 1}, {1, 1}} Output: {{2, 3}, {4, 5}} Input: A[][] = {{2, 4}, {3, 4}} B[][] = {{1, 2}, {1, 3}} Output: {{3, 6}, {4, 7}}Program to Add Two Matrices in JavaFollow the Ste 2 min read Java Program to Check Whether a Given Matrix is Lower Triangular Matrix or Not In the lower triangular matrix, the elements that are present above the diagonal should be 0. Given a matrix and the task is to check the matrix is in lower triangular form or not. A matrix must be a square matrix. A square matrix is called lower triangular if all the entries above the main diagonal 2 min read Java Program to Print Star Pascalâs Triangle Pascalâs triangle is a triangular array of the binomial coefficients. Write a function that takes an integer value n as input and prints the first n lines of Pascalâs triangle. Following are the first 6 rows of Pascalâs Triangle.In this article, we will look into the process of printing Pascal's tri 4 min read Like