-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupper and lower triangle matrix
63 lines (58 loc) · 1.56 KB
/
upper and lower triangle matrix
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Java program to print Lower
// triangular and Upper triangular
// matrix of an array
class GFG
{
// method to form lower
// triangular matrix
static void lower(int matrix[][],
int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i < j)
{
System.out.print("0" + " ");
}
else
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
// Method to form upper
// triangular matrix
static void upper(int matrix[][],
int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i > j)
{
System.out.print("0" + " ");
}
else
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String args[])
{
int matrix[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int row = 3, col = 3;
System.out.println("Lower triangular matrix: ");
lower(matrix, row, col);
System.out.println("Upper triangular matrix: ");
upper(matrix, row, col);
}
}