# Check if a Matrix is Identity Matrix or not in Java?

Matrices are nothing but it’s more than a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.

Identity matrix is a square matrix which has all 1s as its principal diagonal elements and rest are 0s. As per the problem statement we have to0 check if the given matrix is Identity matrix or not.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

## To show you some instances

### Instance-1

Suppose we have a matrix
| 1 0 0 |
A= | 0 1 0 |
| 0 0 1 |


We have 1’s at primary diagonal and rest 0’s. Hence it is an identity matrix

### Instance-2

Suppose we have a matrix
| 1 0 0 |
A =| 0 1 1 |
| 0 0 1 |


We have 1’s at primary diagonal but there is another 1 as non-diagonal element. Hence it is not an identity matrix

### Instance-3

Suppose we have a matrix
| 1 0 0 0|
A = | 0 0 1 0|
| 0 0 1 0|
| 0 0 0 1|


We do not have 1’s at primary diagonal and rest 0’s. Hence it is not an identity matrix

### Algorithm

Step 1 − Store the matrix.

Step 2 −Now call the check function that traverses the matrix and checks if the locations where rows==columns have 1s and rest are 0s.

Step 3 −Print the result.

### Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of Matrix

• By Using Dynamic Initialization of Matrix

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Initialization of Matrix

In this approach, matrix is initialized in the program, then we check if the matrix has 1 at its principal diagonal and rest elements are zero.

### Example

import java.util.Scanner;
public class Main {
public static void main(String[] args) {

// Matrix to be checked
int mat[][] = {
{ 1, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 },
};

// Print results
if (identityMatCheck(mat))
System.out.println("The matrix is an identity matrix.");
else
System.out.println("The matrix is not an identity matrix.");
}

//user defined method
static boolean identityMatCheck(int mat[][]) {

// Traverses the matrix
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {

// Checks if the principal diagonal elemets are 1
if (i == j && mat[i][j] != 1)
return false;

// Checks if the rest elements are 0
else if (i != j && mat[i][j] != 0)
return false;
}
}
return true;
}
}


### Output

The matrix is an Identity matrix.


## Approach-2: By Using Dynamic Initialization of Matrix

In this approach, we ask the user to enter a matrix of their desired size, and and then check if the matrix is identity matrix or not.

### Example

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter number of matrix rows-");
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int mat[][] = new int[size][size];

// Enter Matrix Elements
System.out.println("Enter the matrix elements");
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
mat[i][j] = sc.nextInt();
}
}
// Print results
if (identityMatCheck(mat))
System.out.println("The matrix is an identity matrix.");
else
System.out.println("The matrix is not an identity matrix.");

}
static boolean identityMatCheck(int mat[][]) {
// Traverses the matrix
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {

// Checks if the principal diagonal elemets are 1
if (i == j && mat[i][j] != 1)
return false;
// Checks if the rest elemets are 0
else if (i != j && mat[i][j] != 0)
return false;
}
}
return true;
}
}


### Output

Enter number of matrix rows-
4
Enter the matrix elements
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
The matrix is an identity matrix.


In this article, we explored different approaches to check if the matrix is an identity matrix or not by using Java programming language.