## Java Program to determine whether a given matrix is an identity matrix

In this program, we need to check whether the given matrix is an identity matrix.

Identity Matrix A matrix is said to be an identity matrix if it is a square matrix in which elements of principle diagonal are ones, and the rest of the elements are zeroes.

### Algorithm

• STEP 1: START
• STEP 2: DEFINE rows, cols
• STEP 3: SET flag =true
• STEP 4: INITIALIZE matrix a[][] ={{1,0,0},{0,1,0}, {0,0,1}}
• STEP 5: rows = a.length
• STEP 6: cols = a.length
• STEP 7: if(rows!=cols)
then
PRINT "Matrix should be a square matrix"
else
go to step 8
• STEP 8: REPEAT STEP 9 to STEP 11 UNTIL i<rows
//for(i=0; i<rows; i++)
• STEP 9: REPEAT STEP 10 to STEP 11 UNTIL j<cols
//for(j=0; j<cols; j++)
• STEP 10: if(i==j && a[i][j]== 1) then
SET flag =false
break
• STEP 11: if( i!=j && a[i][j]!=0)
SET flag = false
break
• STEP 12: if(flag)
then PRINT ("Given matrix is an identity matrix")
else
PRINT ("Given matrix is not an identity matrix")
• STEP 13: END

### Program:

Output:

```Given matrix is an identity matrix
```
Next TopicJava Programs   