- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to Display an Identity Matrix in Java?

In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.

An identity matrix is a square matrix where all the elements of the main diagonal (i.e., the diagonal from the top-left to the bottom-right corner) are 1, and all the other elements are 0. It is denoted by the symbol "I" or "In", where "n" represents the size of the matrix.

The identity matrix is important in linear algebra because it acts as the identity element for matrix multiplication, meaning that when a matrix is multiplied by an identity matrix, the result is the original matrix.

Here we have given the matrix length and we have to display an identity matrix of that length.

Let’s start!

## To show you some instances

### Instance-1

Given matrix length = 2

Identity matrix of 2*2 =

1 0 0 1

### Instance-2

Given matrix length = 4

Identity matrix of 2*2 =

1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1

### Instance-3

Given matrix length = 6

Identity matrix of 2*2 =

1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1

## Algorithm

### Algorithm-1

**Step-1**− This program creates an identity matrix of size ‘n’ using a 2D array named identity.**Step-2**− It uses nested for-loops to iterate over each element of the matrix and checks if the current element is on the diagonal (where row index equals column index).**Step-3**− If it is on the diagonal, it sets the element to 1, otherwise, it sets it to 0.**Step-4**− Once the matrix is filled with the appropriate values, it uses another set of nested for-loops to display the matrix on the console.**Step-5**− Finally, it prints a newline after each row is printed to display the matrix in a square shape.

### Algorithm-2

**Step-1**− This program creates an identity matrix of size n using a 2D array named identity.**Step-2**− However, instead of using nested for-loops to fill the diagonal elements with 1 and other elements with 0, it uses the Arrays.fill() method to fill each row of the matrix with 0 initially.**Step-3**− Then, it uses another for-loop to set the diagonal element of each row to 1.**Step-4**− Finally, it uses another set of nested for-loops to display the matrix on the console.**Step-5**− it prints a newline after each row is printed to display the matrix in a square shape.

### Syntax

1. The **length()** method in Java returns the length of the given matrix.

Below refers to the syntax of it −

inputMatrix.lenght

where, ‘inputMatrix’ refers to the given matrix.

2. **IntStream.range()** is a method in Java that generates a sequential stream of integers from startInclusive to endExclusive - 1.

IntStream.range(startInclusive, endExclusive)

It can be used to perform operations on a set of integers. For example, in the second code, it is used to loop through the rows and columns of the matrix.

The method **forEach** is then called on the stream to perform an action for each element in the stream.

## Multiple Approaches

We have provided the solution in different approaches.

By Using Nested for Loops

By Using Arrays.fill() Method

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

## Approach-1: By Using Nested for Loops

In this approach, matrix length will be initialized in the program. Then as per the algorithm using nested for loop display an identity matrix of that length.

### Example

public class Main { public static void main(String[] args) { int n = 5; // size of the matrix int[][] identity = new int[n][n]; // Fill the diagonal elements with 1 and other elements with 0 for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(i == j) identity[i][j] = 1; else identity[i][j] = 0; } } // Display the matrix for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { System.out.print(identity[i][j] + " "); } System.out.println(); // print a newline after each row is printed } } }

### Output

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

## Approach-2: By Using Arrays.fill() Method

In this approach, matrix length will be initialized in the program. Then as per the algorithm using Arrays.fill() method display an identity matrix of that length.

### Example

import java.util.Arrays; public class Main { public static void main(String[] args) { int n = 8; // size of the matrix int[][] identity = new int[n][n]; // Fill the diagonal elements with 1 using Arrays.fill() method for(int i = 0; i < n; i++) { Arrays.fill(identity[i], 0); // fill each row with 0 initially identity[i][i] = 1; // set the diagonal element to 1 } // Display the matrix for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { System.out.print(identity[i][j] + " "); } System.out.println(); // print a newline after each row is printed } } }

### Output

1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1

In this article, we explored different approaches to display an identity matrix by using Java programming language.