C Program to Check Identity Matrix

Here is the C program to check whether a given matrix is an Identity matrix or not. Identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere. An identity matrix is denoted I and an Identity matrix of size n is a n x n square matrix and is denoted by In.

Example of an Identity matrix of size 4
    1 0 0 0
    0 1 0 0
    0 0 1 0
    0 0 0 1
Points to Remember
  • Identity matrix is sometimes also known as unit matrix.
  • Identity matrix of size n is a diagonal matrix of size n with all diagonal elements as one.
  • AI = A : Identity matrix is a matrix whose product with another matrix A equals the same matrix A.
Algorithm to check whether a matrix is an Identity matrix or not
Let inputMatrix be a matrix of size M x N.
  • First of all, check whether number of rows and columns of input matrix are equal or not. If not, they it is not an Identity matrix.
  • Traverse both matrices row wise(first all elements of a row from left to right, then jump to next row) using two loops(check line number 23 and 24 of below program).
  • For any element inputMatrix[i][j], check whether i and j are equal or not.
    • If i == j, it means current element is a diagonal element and it must be 1. If inputMatrix[i][j] is not equal to 1, then inputMatrix is not an Identity matrix.
    • If i != j, it means current element is not a diagonal element and it must be 0 other wise not an Identity matrix.
  • After traversing whole array, If every element of inputMatrix satisfied above mentioned conditions then inputMatrix is identity matrix.

C program to check if a matrix is an identity matrix

Below program first takes a square matrix as input from user. Then it traverses the matrix using two for loops and for every element it checks whether above mentioned conditions are satisfied. Finally, it prints whether input matrix is an Identity matrix or not based upon the algorithm mentioned above.

#include <stdio.h>

int main(){
    int rows, cols, size, rowCounter, colCounter, flag = 1;
    int inputMatrix[50][50];
    
    printf("Enter size of a square matrix\n");
    scanf("%d", &size);
    rows = cols = size;
    
    printf("Enter matrix of size %dX%d\n", rows, cols);
    /* Input matrix */
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            scanf("%d", &inputMatrix[rowCounter][colCounter]);
        }
    }

    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            if(rowCounter == colCounter){
                /* Check if diagonal element is one or not */
                if(inputMatrix[rowCounter][colCounter] != 1){
                    flag = 0;
                }
            } else {
                /* Check if non-diagonal element is zero or not */
                if(inputMatrix[rowCounter][colCounter] != 0){
                    flag = 0;
                } 
            }
        }
    }
    
    if(flag == 0){
        printf("NOT AN IDENTITY MATRIX\n");
    } else {
        printf("IDENTITY MATRIX\n");
    }
    
    return 0;
}
Output
Enter size of a square matrix
3
Enter matrix of size 3X3
1 0 0
0 1 0
0 0 1
IDENTITY MATRIX
Enter size of a square matrix
2
Enter matrix of size 2X2
1 2 
2 1 
NOT AN IDENTITY MATRIX

Related Topics
C program to print lower triangular matrix
C Program for subtraction of two matrix
C Program to find sum of diagonal elements of matrix
C Program for matrix multiplication
C Program to print a matrix diagonally
C program to find scalar multiplication of a matrix
C program to check if two strings are anagram
C program to compare two matrix
List of all C programs