Java Program to Print an Identity Matrix

In the previous article, we have discussed Java Program to Check Whether the Matrix is a Magic Square or Not

In this article we are going to see how we can write a program to print an indentity matrix in JAVA language.

Java Program to Print an Identity 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.

Let’s see different ways to print identity matrix.

Method-1: Java Program to Print an Identity Matrix By Static Initialization of Array Elements

Approach:

  • Ask the user for size.
  • Declare and instantiate an 2D array with the specified size.
  • Set all primary diagonal elements to 1 and rest to 0.
  • Print the matrix.

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);
        
        // Taking matrix size as input
        System.out.println("Enter square matrix row/column size : ");
        int rowSize = scan.nextInt();
        int colSize = rowSize, row, col;

        // Declaring and instantiating  the 3X3 matrix i.e. 2D array
        int arr[][] = new int[rowSize][colSize];

        //Creating the identity matrix
        for(row=0;row<rowSize;row++)
            for(col=0;col<colSize;col++)
                if(row==col)
                    arr[row][col] = 1;
                else
                    arr[row][col] = 0;

        
        System.out.print("The identity matrix elements are : ");
        
        // Loop to print the elements
        for(row=0;row<rowSize;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<colSize;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
    }
}

Output:

Enter square matrix row/column size : 3
The identity matrix elements are : 
1 0 0 
0 1 0 
0 0 1

Method-2: Java Program to Print an Identity Matrix By Using User Defined Methods

Approach:

  • Ask the user for size.
  • Declare and instantiate an 2D array with the specified size.
  • Call an user defined method to print identity matrix.
  • Within the user defined method, Set all primary diagonal elements to 1 and rest to 0.
  • Print the matrix.

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);
            
            // Taking matrix size as input
            System.out.println("Enter square matrix row/column size : ");
            int rowSize = scan.nextInt();
            int colSize = rowSize, row, col;
    
            // Declaring and instantiating  the 3X3 matrix i.e. 2D array
            int arr[][] = new int[rowSize][colSize];
            
            //calling the user defined method
            //to print identity matrix
            identityMatrix(arr,rowSize,colSize);
            
            System.out.print("The identity matrix elements are : ");
            //method called to print the matrix
            printMatrix(arr,rowSize,colSize);
        }
        
        //creating the identoity matrix
        public static void identityMatrix(int arr[][],int rowSize,int colSize)
        {
        //Creating the identity matrix
        for(int row=0;row<rowSize;row++)
            for(int col=0;col<colSize;col++)
                if(row==col)
                    arr[row][col] = 1;
                else
                    arr[row][col] = 0;
        }
        
        
        // Method to print the matrix
        static void printMatrix(int arr[][],int rowSize,int colSize)
        {
            int row, col;
            // Loop to print the elements
            for(row=0;row<rowSize;row++)
            {
                // Used for formatting
                System.out.print("\n");
                for(col=0;col<colSize;col++)
                {
                    System.out.print(arr[row][col]+" ");
                }
            }
             System.out.print("\n");
        }
    
}

Output:

Enter square matrix row/column size : 
The identity matrix elements are : 
1 0 0 0 0 
0 1 0 0 0 
0 0 1 0 0 
0 0 0 1 0 
0 0 0 0 1

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: