Javatpoint Logo
Javatpoint Logo

Program to determine whether a given matrix is an identity matrix

Explanation

In this program, we need to check whether the given matrix is an identity matrix.

Identity Matrix

Program to determine whether a given matrix is an identity matrix

A matrix is said to be the identity matrix if it is the square matrix in which elements of principle diagonal are ones, and the rest of the elements are zeroes.

Algorithm

  1. Declare and initialize a two-dimensional array a.
  2. Calculate the number of rows and columns present in the array and store it in variables rows and columns respectively.
  3. Initialize variable flag to true.
  4. Check if given matrix has the same number of rows and columns(square matrix).
  5. If not, print the error message "Matrix should be a square matrix."
  6. If given matrix is a square matrix then, loop through the array and check if all the elements of main diagonal are 1 and the rest of the elements are 0.
  7. If any of the condition is not satisfied, set the flag to false and break the loop.
  8. If the flag is equal to true which implies given matrix is an identity matrix.
  9. Else, given matrix is not an identity matrix.

Solution

Python

Output:

Given matrix is an identity matrix

C

Output:

Given matrix is an identity matrix

JAVA

Output:

Given matrix is an identity matrix

C#

Output:

Given matrix is an identity matrix

PHP

Output:

Given matrix is an identity matrix

Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA