Java Program to Check Whether a Given Matrix is an Identity Matrix

In the previous article, we have discussed Java Program to Check Whether a Given Matrix is a Sparse Matrix

In this article we are going to see how we can write a program to check if a matrix is an identity matrix in JAVA language.

Java Program to Check Whether Two Matrices are Equal or Not

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=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Identity matrix is a matrix which has ones(1s) as its principal diagonal elements and rest elements are zeroes(0s).

                                1  0  0
Identity Matrix =     0  1  0
                                0  0  1

Let’s see different ways to check whether a given matrix is an Identity matrix or not.

Method-1: Java Program to Check Whether a Given Matrix is an Identity Matrix By Static Initialization of Array Elements

Approach:

  • Initialize and declare an array with elements.
  • Using two for loops to iterate the rows and columns. Then check whether all primary diagonal elements are 1 and non-primary diagonal elements are 0.
  • If the conditions are true , then it is a identity matrix, else not .

Program:

public class matrix

{
    public static void main(String args[])
    {        
        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = {{1,0,0},{0,1,0},{0,0,1}};
        int row, col;
        boolean flag = true;

        System.out.print("The matrix elements are:");
        printMatrix(arr);

        // Loops to find transpose of the matrix
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {   
                // Checks wether the primary diagonal elements are 1 or not
                if(row==col)
                {
                    if(arr[row][col]!=1)
                    {
                        flag = false;
                        break;
                    }
                }
                // Checks wether the non-primary diagonal elements are 0 or not
                else
                {
                    if(arr[row][col]!=0)
                    {
                        flag = false;
                        break;
                    }
                }
                
            }

        if(flag)
            System.out.println("\nIt is an identity matrix");
        else
            System.out.println("\nIt is not an identity matrix");
    }

    // Function to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }
}
Output:

The matrix elements are:
1 0 0 
0 1 0 
0 0 1

It is an identity matrix

Method-2: Java Program to Check Whether a Given Matrix is an Identity Matrix By Dynamic Initialization of Array Elements

Approach:

  • Initialize two arrays of size 3×3.
  • Ask the user for input off array elements and store them in the arrays using two for loops.
  • Using two for loops to iterate the rows and columns. Then check whether all primary diagonal elements are 1 and non-primary diagonal elements are 0.
  • If the conditions are true , then it is a identity matrix, else not .

Program:

import java.util.Scanner;
public class matrix{
    public static void main(String args[])
    {        
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = new int[3][3];
        int row, col;
        boolean flag = true;

        // Taking matrix input
        System.out.println("\nEnter the matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                arr[row][col] = scan.nextInt();

        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        // Loops to find transpose of the matrix
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {   
                // Checks wether the primary diagonal elements are 1 or not
                if(row==col)
                {
                    if(arr[row][col]!=1)
                    {
                        flag = false;
                        break;
                    }
                }
                // Checks wether the non-primary diagonal elements are 0 or not
                else
                {
                    if(arr[row][col]!=0)
                    {
                        flag = false;
                        break;
                    }
                }
                
            }

        if(flag)
            System.out.println("\nIt is an identity matrix");
        else
            System.out.println("\nIt is not an identity matrix");
    }

    // Function to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }
}
Output:


Enter the matrix elements : 1  0 0 0 1 0 0 0 1
The matrix elements are : 
1 0 0 
0 1 0 
0 0 1

It is an identity 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: