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
An identity matrix has 1s on the main diagonal and 0s elsewhere, so the nested loops check each element against this rule and set a flag to false on any violation, demonstrating identity matrix validation.