Identity Matrix Check

This program checks if a matrix is an identity matrix.

Problem Statement

Write a Java program to check whether a 3x3 matrix is an identity matrix.

Source Code

1 public class IdentityMatrixCheck {
2 public static void main(String[] args) {
3 int[][] A = {{1,0,0},{0,1,0},{0,0,1}};
4 boolean identity=true;
5 for(int i=0;i<3;i++){
6 for(int j=0;j<3;j++){
7 if(i==j && A[i][j]!=1) identity=false;
8 else if(i!=j && A[i][j]!=0) identity=false;
9 }
10 }
11 if(identity) System.out.println("Matrix is identity");
12 else System.out.println("Matrix is not identity");
13 }
14 }

Program Output

Matrix is identity

Explanation

Identity matrix has 1s on main diagonal and 0s elsewhere. - Nested loops check each element. - If diagonal not 1 or off diagonal not 0, flag set false. - Together demonstrates identity matrix validation. - Shows how multidimensional arrays can be validated against conditions. Conclusion: Demonstrates identity matrix check using arrays.

Quick Links to Explore