# Java Program to Check Idempotent Matrix

In the previous article, we have seen Java Program to Find the Product of Middle Row and Middle Column of a Matrix

In this article we are going to see how we can write a program to check whether matrix is Idempotent Matrix or not.

## Java Program to Check Idempotent Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
| A20   A21   A22 | 3*3
• Matrix A represents a 3*3 matrix.
• A‘ represents the matrix element
• Aij‘ represents the matrix element at it’s matrix position/index.
• i‘ represents the row index
• j‘ represents the column index
• Means A00=Aij  where i=0 and j=0,  A01=aij where i=0 and j=1 and like this.
• Here we have started row value from 0 and column value from 0.

A matrix whose product of matrix to itself is equal to that matrix is called idempotent matrix .

Let’s see different ways to to check whether matrix is Idempotent Matrix or not.

### Method-1: Java Program to Check Idempotent Matrix By Static Initialization of Array Elements

Approach :

• Declare and initialize a matrix.
• Calculate the product to itself .
• Check the product of the matrix and the original matrix same or not .

### Program :

import java.util.*;

public class Main
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
// Initializing the 3X3 matrix i.e. 2D array
int mat[][]={{2,-2,-4},{-1,3,4},{1,-2,-3}};
int res[][]=new int[3][3];;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
res[i][j] = 0;
for (int k = 0; k < 3; k++)
res[i][j] += mat[i][k] * mat[k][j];
}
}
for(int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (mat[i][j] != res[i][j])
{
System.out.print("Entered matrix not an idempotent matrix .");
System.exit(0);
}
System.out.println("Entered matrix is an idempotent matrix");

}
}


Output:

Entered matrix is an idempotent matrix

### Method-2: Java Program to Check Idempotent Matrix By Dynamic Initialization of Array Elements

• Take input of a matrix from user.
• Calculate the product to itself .
• Check the product of the matrix and the original matrix same or not .
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
// Initializing the 3X3 matrix i.e. 2D array
int mat[][] = new int[3][3];
int row, col ;
// Taking matrix input
System.out.println("\nEnter matrix elements");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
mat[row][col] = s.nextInt();
int res[][]=new int[3][3];;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
res[i][j] = 0;
for (int k = 0; k < 3; k++)
res[i][j] += mat[i][k] * mat[k][j];
}
}
for(int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (mat[i][j] != res[i][j])
{
System.out.print("Entered matrix not an idempotent matrix .");
System.exit(0);
}
System.out.println("Entered matrix is an idempotent matrix");

}
}


Output:

Enter matrix elements
2 -2 -4
-1 3 4
1 -2 -3
Entered matrix is an idempotent matrix

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: