-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpperTriangularMatrix.java
More file actions
63 lines (52 loc) · 1.55 KB
/
UpperTriangularMatrix.java
File metadata and controls
63 lines (52 loc) · 1.55 KB
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
/*
* PROGRAM : To find the upper triangular matrix
* FILE : UpperTriangularMatrix.java
* CREATED BY : Santosh Hembram
* DATE : 12-10-20
*/
import java.util.*;
class UpperTriangularMatrix {
public static void upperTriangular(int mat[][],int r,int c){
System.out.println("-------- Displaying the upper triangular matrix ---------");
for (int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
if (i<=j) {
System.out.print(mat[i][j]+" ");
}
else {
System.out.print("0"+" ");
}
}
System.out.println();
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the row size: ");
int r = sc.nextInt();
System.out.print("Enter the coloumn size: ");
int c = sc.nextInt();
while (r!=c) {
System.out.println("WARNING!!!");
System.out.println("The row and coloumn size must be equal.");
System.out.print("re-Enter the coloumn size : ");
c = sc.nextInt();
}
int mat[][] = new int[r][c];
System.out.println("---------- Enter the elements of the matrix --------------");
for(int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
System.out.print("Enter the elements for row "+i+" coloumn "+j+": ");
mat[i][j] = sc.nextInt();
}
}
System.out.println("---------- Displaying the matrix --------------");
for(int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
upperTriangular(mat,r,c);
}
}