public class IdentityMatrix {
public static void main(String[] args) {
int[][] matrix = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
boolean isIdentity = true;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (i == j && matrix[i][j] != 1) {
isIdentity = false;
break;
}
if (i != j && matrix[i][j] != 0) {
isIdentity = false;
break;
}
}
}
if (isIdentity) {
System.out.println("The given matrix is an identity matrix.");
} else {
System.out.println("The given matrix is not an identity matrix.");
}
}
}
The given matrix is an identity matrix.matrix represented as a two-dimensional array. We initialize a boolean variable isIdentity to true assuming that the matrix is an identity matrix.matrix. The outer loop iterates over the rows, and the inner loop iterates over the columns. For each element in the matrix, we check whether it is on the main diagonal (i.e., the element at row i and column j where i == j). If it is on the main diagonal, we check whether it is equal to 1. If it is not on the main diagonal, we check whether it is equal to 0. If either of these conditions is not satisfied, we set isIdentity to false and break out of both loops.matrix is an identity matrix or not.