This is a program to check if a given matrix is an Identity Matrix

This C Program checks a given Matrix is an Identity Matrix. Identity matrix is a square matrix with 1’s along the diagonal from upper left to lower right and 0’s in all other positions. If it satisfies the structure as explained before then the matrix is called as identity matrix.

1. Create a matrix (2D array) and define its elements according to its size.

2. Two check whether the matrix is an identity matrix or not, run a nested for loop with two iterators.

3. Using two iterators, locate each position of matrix and check whether the right diagonal have all 1s with remaining elements as 0s.

4. Set a flag variable 1 if it matches the above condition, otherwise it will be 0.

5. Print Identity matrix if flag is 1.

Here is source code of the C program to check a given Matrix is an Identity Matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* C Program to check if a given matrix is an identity matrix`

`*/`

`#include <stdio.h>`

void main()

`{`

int a[10][10];

int i, j, row, column, flag = 1;

printf("Enter the order of the matrix A \n");

scanf("%d %d", &row, &column);

printf("Enter the elements of matrix A \n");

for (i = 0; i < row; i++)

`{`

for (j = 0; j < column; j++)

`{`

scanf("%d", &a[i][j]);

`}`

`}`

printf("MATRIX A is \n");

for (i = 0; i < row; i++)

`{`

for (j = 0; j < column; j++)

`{`

printf("%3d", a[i][j]);

`}`

printf("\n");

`}`

`/* Check for unit (or identity) matrix */`

for (i = 0; i < row; i++)

`{`

for (j = 0; j < column; j++)

`{`

if (a[i][j] != 1 && a[j][i] != 0)

`{`

flag = 0;

break;

`}`

`}`

`}`

if (flag == 1 )

printf("It is identity matrix \n");

`else`

printf("It is not a identity matrix \n");

`}`

1. Declare a 2D array (matrix), of some fixed capacity.

2. Take input from users the number of rows and columns. Accordingly define the elements of the array.

3. Now, run a nested for loop with 2 iterators, i and j which will help to locate each element of the array.

4. Inside the loop, create a condition which checks whether the element at right diagonal is 1 and remaining elements are 0s or not.

5. A variable flag is created and is set to 1 initially. This flag is set to 0 if it does not satisfy the above condition.

6. Appropriate message is printed according to the value of flag.

Enter the order of the matrix A 3 3 Enter the elements of matrix A 1 2 3 5 1 8 6 4 1 MATRIX A is 1 2 3 5 1 8 6 4 1 It is not a identity matrix $ a.out Enter the order of the matrix A 3 3 Enter the elements of matrix A 1 0 0 0 1 0 0 0 1 MATRIX A is 1 0 0 0 1 0 0 0 1 It is identity matrix

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.