# Program to Determine Whether a Given Matrix is an Identity Matrix

Program to Determine Whether a Given Matrix is an Identity Matrix on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

## 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

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.

### Python

```#Initialize matrix a
a = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
];

flag = True;

#Calculates number of rows and columns present in given matrix
rows = len(a);
cols = len(a[0]);

#Checks whether given matrix is a square matrix or not
if(rows != cols):
print("Matrix should be a square matrix");

else:
#Checks if diagonal elements are equal to 1 and rest of elements are 0
for i in range(0, rows):
for j in range(0, cols):
if(i == j and a[i][j] != 1):
flag = False;
break;

if(i != j and a[i][j] != 0):
flag = False;
break;

if(flag):
print("Given matrix is an identity matrix");
else:
print("Given matrix is not an identity matrix");
```

Output:

```Given matrix is an identity matrix
```

### C

```#include <stdio.h>
#include <stdbool.h>

int main()
{
int rows, cols;
bool flag = true;

//Initialize matrix a
int a[][3] = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};

//Calculates number of rows and columns present in given matrix
rows = (sizeof(a)/sizeof(a[0]));
cols = (sizeof(a)/sizeof(a[0][0]))/rows;

//Checks whether given matrix is a square matrix or not
if(rows != cols){
printf("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)
printf("Given matrix is an identity matrix");
else
printf("Given matrix is not an identity matrix");
}
return 0;
}
```

Output:

```Given matrix is an identity matrix
```

### JAVA

```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
```

### C#

```using System;

public class IdentityMatrix
{
public static void Main()
{
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.GetLength(0);
cols = a.GetLength(1);

//Checks whether given matrix is a square matrix or not
if(rows != cols){
Console.WriteLine("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)
Console.WriteLine("Given matrix is an identity matrix");
else
Console.WriteLine("Given matrix is not an identity matrix");
}
}
}
```

Output:

```Given matrix is an identity matrix
```

### PHP

```<!DOCTYPE html>
<html>
<body>
<?php
//Initialize matrix a
\$a = array(
array(1, 0, 0),
array(0, 1, 0),
array(0, 0, 1)
);

\$flag = true;

//Calculates number of rows and columns present in given matrix
\$rows = count(\$a);
\$cols = count(\$a[0]);

//Checks whether given matrix is a square matrix or not
if(\$rows != \$cols){
print("Matrix should be a square matrix <br>");
}
else {
//Checks if diagonal elements are equal to 1 and rest of elements are 0
for(\$i = 0; \$i < \$rows; \$i++){
for(\$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)
print("Given matrix is an identity matrix <br>");
else
print("Given matrix is not an identity matrix <br>");
}
?>
</body>
</html>
```

Output:

```Given matrix is an identity matrix
```

Next Topic#