Write a C program to read elements in a matrix and check whether matrix is an Identity matrix or not. C program for finding Identity matrix.

1 0 0

0 1 0

0 0 1

Output: It is an Identity matrix.

If

Else

Happy coding ;)

**Example:**If elements of a 3x3 matrix are:1 0 0

0 1 0

0 0 1

Output: It is an Identity matrix.

### Required knowledge:

Basic C programming, For loop, Array, Matrix#### Identity Matrix:

Identity matrix is a special square matrix whose main diagonal elements is equal to 1 and other elements are 0. Identity matrix is also known as unit matrix.### Algorithm to check Identity matrix:

For checking a matrix**A**we need to ensure thatIf

*i = j*then**A**_{ij}must be equal to 1.Else

**A**_{ij}must be equal to 0. (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n)### Program:

/** * C program to check whether a matrix is Identity matrix or not */ #include <stdio.h> int main() { int A[3][3]; int row, col, isIdentity; /* * Reads elements in matrix from user */ printf("Enter elements in matrix of size 3x3: \n"); for(row=0; row<3; row++) { for(col=0; col<3; col++) { scanf("%d", &A[row][col]); } } /* * Checks whether it is Identity matrix or not */ isIdentity = 1; for(row=0; row<3; row++) { for(col=0; col<3; col++) { if(row==col && A[row][col]!=1) { /* If elements of main diagonal is not equal to 1 */ isIdentity = 0; } else if(row!=col && A[row][col]!=0) { /* If other elements than main diagonal is not equal to 0 */ isIdentity = 0; } } } /* * If it is an Identity matrix */ if(isIdentity==1) { printf("\nThe given matrix is an Identity Matrix.\n"); /* * Prints the Identity matrix */ for(row=0; row<3; row++) { for(col=0; col<3; col++) { printf("%d ", A[row][col]); } printf("\n"); } } else { printf("The given matrix is not Identity Matrix"); } return 0; }

Output

Enter elements in matrix of size 3x3:

1 0 0

0 1 0

0 0 1

The given matrix is an Identity Matrix.

1 0 0

0 1 0

0 0 1

1 0 0

0 1 0

0 0 1

The given matrix is an Identity Matrix.

1 0 0

0 1 0

0 0 1

Happy coding ;)

### You may also like

- Array and Matrix programming exercises index.
- C program to check Sparse matrix.
- C program to check Symmetric matrix.
- C program to find transpose of a matrix.
- C program to find determinant of a matrix.
- C program to add two matrices.
- C program to subtract two matrices.
- C program to multiply two matrices.
- C program to perform scalar matrix multiplication.
- C program to print Prime numbers between 1 to n.
- C program to print Armstrong numbers between 1 to n.
- C program to print Strong numbers between 1 to n.
- C program to print Perfect numbers between 1 to n.
- C program to print Fibonacci series up to n terms.
- C program to print Pascal triangles up to n rows.

< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)