This is the Java Program to Display Upper/Lower Triangle of a Matrix.
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
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].
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.
//Java Program to Display Upper/Lower Triangle of a Matrix
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class UpperAndLowerTriangle {
// Function to display upper and lower triangle
static void displayUpperAndLowerTriangle(int[][] matrix){
int order = matrix.length;
int i,j;
for(i=0; i<order; i++){
for(j=0; j<order;j++){
if((i+j) <order)
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
for(i=0; i<order; i++){
for(j=0; j<order;j++){
if((i+j) >=order)
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
// Function to read user input
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int order;
System.out.println("Enter the order of the matrix");
try{
order = Integer.parseInt(br.readLine());
}
catch(Exception e){
System.out.println("An error occurred");
return;
}
int[][] matrix = new int[order][order];
System.out.println("Enter matrix elements");
int i,j;
for(i=0; i<order; i++){
for(j=0; j<order; j++){
try{
matrix[i][j] = Integer.parseInt(br.readLine());
}
catch(Exception e){
System.out.println("An error occurred");
return;
}
}
}
System.out.println("Tha matrix is");
for(i=0; i<order; i++){
for(j=0; j<order; j++){
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
System.out.println("The upper and lower triangle is");
displayUpperAndLowerTriangle(matrix);
}
}
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.
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.
- Practice BCA MCQs
- Check Java Books
- Practice Information Technology MCQs
- Practice Programming MCQs
- Check Programming Books