-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaArray09.txt
More file actions
33 lines (30 loc) · 1.5 KB
/
JavaArray09.txt
File metadata and controls
33 lines (30 loc) · 1.5 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
/*You are given a square matrix A of size N×N. Check whether the given matrix is an Identity matrix or not. If it is, then print "Identity matrix" or otherwise print "Not an Identity matrix". Your program should work for any given 2D Array of size N×N.
[You may need to use the concept of flag and break to solve this problem.]
Identity Matrix is a square matrix with 1’s along the diagonal from upper left to lower right and 0’s in all other positions.
Given Array Output
int [ ] [ ] A = {{1, 0, 0, 1}, Not an Identity Matrix
{0, 1, 0, 0},
{1, 0, 1, 0},
{0, 1, 0, 1}};
*/
import java.util.Scanner;
public class Task09{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
int n= sc.nextInt();
int [] [] arr = new int [n] [n];
boolean flag=false;
for(int i=0;i<arr.length;i++){
for(int j=0;j<n;j++){
arr[i][j]=sc.nextInt();}}
for(int i=0;i<arr.length;i++){
for(int j=0;j<n;j++){
if((i==j && arr[i][j]!=1)||(i!=j && arr[i][j]!=0)){
flag=true;
break;}}}
if(flag){
System.out.println("Not an Identity Matrix");}
else{
System.out.println("Identity Matrix");}
}
}