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

int main (void)

`{`

int a[10][10];

int i = 0, j = 0, row = 0, col = 0;

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

printf ("where m = number of rows; and\n");

printf (" n = number of columns\n");

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

int flag = 0;

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

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

`{`

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

`{`

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

`}`

`}`

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

`{`

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

`{`

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

`{`

flag = -1;

break;

`}`

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

`{`

flag = -1;

break;

`}`

`}`

`}`

if (flag == 0)

`{`

printf ("It is a IDENTITY MATRIX\n");

`}`

`else`

`{`

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

`}`

return 0;

`}`

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 (mxn): where m = number of rows; and n = number of columns 3 3 Enter the elements of the matrix 1 0 0 0 1 0 0 0 1 It is a IDENTITY MATRIX Enter the order of the matrix (mxn): where m = number of rows; and n = number of columns 3 3 Enter the elements of the matrix 1 2 3 4 5 6 5 8 7 It is NOT an identity matrix

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

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

**Next Steps:**

- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Practice Computer Science MCQs
- Buy Programming Books
- Apply for Computer Science Internship
- Apply for Information Technology Internship
- Apply for Data Structure Internship