public class UpperTriangularMatrix {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printUpperTriangularMatrix(matrix);
}
public static void printUpperTriangularMatrix(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
1 2 3
5 6
9
matrix
represented as a two-dimensional array. We define a method printUpperTriangularMatrix
that takes the matrix as input and prints its upper triangular matrix to the console.