-
Notifications
You must be signed in to change notification settings - Fork 25
/
UpperTraingular.java
34 lines (31 loc) · 1.15 KB
/
UpperTraingular.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class UpperTriangular
{
public static void main(String[] args) {
int rows, cols;
//Initialize matrix a
int a[][] = {
{1, 2, 3},
{8, 6, 4},
{4, 5, 6}
};
//Calculates number of rows and columns present in given matrix
rows = a.length;
cols = a[0].length;
if(rows != cols){
System.out.println("Matrix should be a square matrix");
}
else {
//Performs required operation to convert given matrix into upper triangular matrix
System.out.println("Upper triangular matrix: ");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(i > j)
System.out.print("0 ");
else
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
}