-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathCheckIdentity.java
44 lines (39 loc) · 1.24 KB
/
CheckIdentity.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
35
36
37
38
39
40
41
42
43
44
/**
* Check Identity Matrix
* @author MadhavBahl
* @date 26/01/2019
*/
import java.util.*;
class CheckIdentity {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the N (for NxN matrix): ");
int n = input.nextInt();
// Input the array
int[][] arr = new int[n][n];
System.out.println("Enter the elements: ");
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
System.out.print("arr[" + i + "][" + j + "] = ");
arr[i][j] = input.nextInt();
}
}
// Check for identity
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (i == j) {
if (arr[i][j] != 1) {
System.out.println("Given matrix is not identity!");
System.exit(0);
}
} else {
if (arr[i][j] != 0) {
System.out.println("Given matrix is not identity!");
System.exit(0);
}
}
}
}
System.out.println("Given matrix is an identity matrix!");
}
}