Java Program to Display Upper and Lower Triangular Matrix

This is the Java Program to Display Upper/Lower Triangle of a Matrix.

Problem Description

Given a square matrix, print it’s upper and lower triangle.
Example:
Matrix:
0 0 1 1
0 1 1 1
0 0 0 1
0 0 0 0

Output:
Upper Triangle
0 0 1 1
0 1 1
0 0
0

Lower Triangle
       1
    0 1
 0 0 0

Problem Solution

Traverse through the matrix. To display the upper triangle check whether the sum of i and j indexes is less than the order of the matrix if it is then print the element matrix[i][j]. Similarly, to display the lower triangle check whether the sum of i and j indexes is greater than or equal to the order of the matrix if it is then print the element matrix[i][j].

Program/Source Code

Here is the source code of the Java Program to Display Upper/Lower Triangle of a Matrix. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

advertisement
advertisement
  1.  
  2. //Java Program to Display Upper/Lower Triangle of a Matrix
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class UpperAndLowerTriangle {
  8.     // Function to display upper and lower triangle
  9.     static void displayUpperAndLowerTriangle(int[][] matrix){
  10.         int order = matrix.length;
  11.         int i,j;
  12.         for(i=0; i<order; i++){
  13.             for(j=0; j<order;j++){
  14.                 if((i+j) <order)
  15.                     System.out.print(matrix[i][j] + "\t");
  16.             }
  17.             System.out.println();
  18.         }
  19.         for(i=0; i<order; i++){
  20.             for(j=0; j<order;j++){
  21.                 if((i+j) >=order)
  22.                     System.out.print(matrix[i][j] + "\t");
  23.             }
  24.             System.out.println();
  25.         }
  26.     }
  27.     // Function to read user input
  28.     public static void main(String[] args) {
  29.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  30.         int order;
  31.         System.out.println("Enter the order of the matrix");
  32.         try{
  33.             order = Integer.parseInt(br.readLine());
  34.         }
  35.         catch(Exception e){
  36.             System.out.println("An error occurred");
  37.             return;
  38.         }
  39.         int[][] matrix = new int[order][order];
  40.         System.out.println("Enter matrix elements");
  41.         int i,j;
  42.         for(i=0; i<order; i++){
  43.             for(j=0; j<order; j++){
  44.                 try{
  45.                     matrix[i][j] = Integer.parseInt(br.readLine());
  46.                 }
  47.                 catch(Exception e){
  48.                     System.out.println("An error occurred");
  49.                     return;
  50.                 }
  51.             }
  52.         }
  53.         System.out.println("Tha matrix is");
  54.         for(i=0; i<order; i++){
  55.             for(j=0; j<order; j++){
  56.                 System.out.print(matrix[i][j] + "\t");
  57.             }
  58.             System.out.println();
  59.         }
  60.         System.out.println("The upper and lower triangle is");
  61.         displayUpperAndLowerTriangle(matrix);
  62.     }
  63. }
Program Explanation

1. In function displayUpperAndLowerTriangle(), the order variable is initialized to matrix.length, which is the order of the matrix passed.
2. Within the first set of nested loops, the condition if((i+j) < order) is checked to display the upper triangle of the matrix.
3. Within the second set of nested loops, the condition if((i+j) >= order) is checked to display the lower triangle of the matrix.

Time Complexity: O(nm) where n is the number of rows and m is the number of columns in the matrix respectively.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the order of the matrix
4
Enter matrix elements
0
0
1
1
0
1
1
1
0
0
0
1
0
0
0
0
The matrix is
0	0	1	1	
0	1	1	1	
0	0	0	1	
0	0	0	0	
The upper and lower triangle is
0	0	1	1	
0	1	1	
0	0	
0	
 
			1	
		0	1	
	0	0	0
 
Case 2 (Simple Test Case - another example):
 
Enter the order of the matrix
3
Enter matrix elements
1
2
3
4
5
6
7
8
9
The matrix is
1	2	3	
4	5	6	
7	8	9	
The upper and lower triangle is
1	2	3	
4	5	
7	
 
		6	
	8	9

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.