Home >>Java Programs >Java Program to check a given matrix is an identity matrix
In this example, we will create a Java program to check whether the given matrix is an identity matrix or not.
Any given matrix is said to be an identity matrix if that matrix is a square matrix in which elements of principle diagonal are ones and the rest of the elements are zeroes.
Program:
public class Main
{
public static void main(String[] args)
{
int rows, cols;
boolean flag = true;
int a[][] = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};
rows = a.length;
cols = a[0].length;
if(rows != cols)
{
System.out.println("Matrix should be a square matrix");
}
else
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
if(i == j && a[i][j] != 1)
{
flag = false;
break;
}
if(i != j && a[i][j] != 0)
{
flag = false;
break;
}
}
}
if(flag)
System.out.println("Given matrix is an identity matrix");
else
System.out.println("Given matrix is not an identity matrix");
}
}
}