Skip to content

Commit 0e510d0

Browse files
authored
Add files via upload
1 parent 9711ae9 commit 0e510d0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

JavaArray09.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*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.
2+
[You may need to use the concept of flag and break to solve this problem.]
3+
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.
4+
5+
Given Array Output
6+
int [ ] [ ] A = {{1, 0, 0, 1}, Not an Identity Matrix
7+
{0, 1, 0, 0},
8+
{1, 0, 1, 0},
9+
{0, 1, 0, 1}};
10+
*/
11+
12+
import java.util.Scanner;
13+
public class Task09{
14+
public static void main(String [] args){
15+
Scanner sc= new Scanner(System.in);
16+
int n= sc.nextInt();
17+
int [] [] arr = new int [n] [n];
18+
boolean flag=false;
19+
for(int i=0;i<arr.length;i++){
20+
for(int j=0;j<n;j++){
21+
arr[i][j]=sc.nextInt();}}
22+
for(int i=0;i<arr.length;i++){
23+
for(int j=0;j<n;j++){
24+
if((i==j && arr[i][j]!=1)||(i!=j && arr[i][j]!=0)){
25+
flag=true;
26+
break;}}}
27+
if(flag){
28+
System.out.println("Not an Identity Matrix");}
29+
else{
30+
System.out.println("Identity Matrix");}
31+
}
32+
}
33+

0 commit comments

Comments
 (0)