# Java Program to Print Upper Triangle Matrix Excluding Principal Diagonal Matrix

This Java program prints values of upper triangle matrix. The program considers only upper triangle excluding principal diagonal elements of the matrix.

**Program**

```
package triangle;
import java.io.*;
import java.util.*;
public class PrintUpperTriangularMatrix {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter the number of rows:");
int row = Integer.parseInt(br.readLine());
System.out.println("Enter the number of columns:");
int col = Integer.parseInt(br.readLine());
int mat[][] = new int[row][col];
System.out.println("Enter " + (row * col) + " values into matrix");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
mat[i][j] = Integer.parseInt(br.readLine());
}
}
System.out.println("Values in upper triangular matrix is:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i < j)
System.out.print(mat[i][j] + "\t");
else
System.out.print("*" + "\t");
}
System.out.println();
}
}
}
```

Let us understand the program by breaking into simple parts: An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. It extends the abstract class Reader.

The above program prompts the user to enter the number of rows and columns for the matrix. It reads these values from the standard input (keyboard) using `BufferedReader`

and `readLine()`

method, and converts them into integers using `Integer.parseInt()`

. 2D array mat[][] is created to store the matrix data, with dimensions determined by the user-input row and column values. Then the user is again prompted to input values for each cell of the matrix.

Nested for loops are used to take inputs from the user for each cell and save it in the form of array. After each cell value of the matrix is collected, the program uses the nested for loop and prints upper triangular part of the matrix excluding principal diagonal elements.

For each cell, it checks if the row index `i is lesser than the column index j`

. If so, it prints the value from the matrix; otherwise, it prints a `"*"`

indicating it's not part of the upper triangular portion. The upper triangular part of the matrix consists of the elements where the row index is lesser than or equal to the column index.

**Output**

```
Enter the number of rows:
3
Enter the number of columns:
3
Enter 9 values into matrix
65
41
10
34
27
50
71
83
94
Values in upper triangular matrix is:
* 41 10
* * 50
* * *
```