Java Program to Check if a Matrix is an Identity Matrix

An identity matrix is defined as a square matrix where the elements along the principal diagonal are ones, and all other elements are zeros. In Java, we can determine if a given matrix is an identity matrix by examining its dimensions and verifying the specific pattern of elements. By checking if the principal diagonal contains only ones and all other elements are zeros.

Algorithm to Check if a Matrix is an Identity Matrix

    • STEP 1: START
    • STEP 2: DEFINE rows, cols
    • STEP 3: SET flag =true
    • STEP 4: INITIALIZE matrix a[][] ={{1,0,0},{0,1,0}, {0,0,1}}
    • STEP 5: rows = a.length
    • STEP 6: cols = a[0].length
    • STEP 7: if(rows!=cols)
      then
      PRINT “Matrix should be a square matrix”
      else
      go to step 8
    • STEP 8: REPEAT STEP 9 to STEP 11 UNTIL i<rows
      //for(i=0; i<rows; i++)
    • STEP 9: REPEAT STEP 10 to STEP 11 UNTIL j<cols
      //for(j=0; j<cols; j++)
    • STEP 10: if(i==j && a[i][j]== 1) then
      SET flag =false
      break
    • STEP 11: if( i!=j && a[i][j]!=0)
      SET flag = false
      break
    • STEP 12: if(flag)
      then PRINT (“Given matrix is an identity matrix”)
      else
      PRINT (“Given matrix is not an identity matrix”)
    • STEP 13: END

Java Program to Check if a Matrix is an Identity Matrix

public class IdentityMatrix     
{    
    public static void main(String[] args) {    
        int rows, cols;    
        boolean flag = true;    
            
        //Initialize matrix a    
        int a[][] = {       
                        {1, 0, 0},    
                        {0, 1, 0},    
                        {0, 0, 1}    
                    };    
            
        //Calculates the number of rows and columns present in the given matrix    
    
          rows = a.length;    
        cols = a[0].length;    
            
        //Checks whether given matrix is a square matrix or not    
        if(rows != cols){    
            System.out.println("Matrix should be a square matrix");    
        }    
        else {    
            //Checks if diagonal elements are equal to 1 and rest of elements are 0    
            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");    
        }    
    }    
}

Output:

Given matrix is an identity matrix

In conclusion, the provided Java program effectively checks if a matrix is an identity matrix by verifying if all diagonal elements are equal to 1 and all non-diagonal elements are equal to 0. This program accurately determines whether a given matrix satisfies the criteria of an identity matrix in Java. Follow tutorials.freshersnow.com to learn more.