C Program to Check Whether a Matrix is an Identity Matrix
Subject: PPS (Programming for Problem Solving)
Contributed By: Sanjay
Created At: February 3, 2025
Question:
Write a C Program to Check Whether a Matrix is an Identity Matrix
Explanation Video:

Explanation:
An identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere. The program checks if all diagonal elements are 1 and all other elements are 0.
Source Code:
#include <stdio.h>
int main() {
int arr[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
int isIdentity = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if ((i == j && arr[i][j] != 1) || (i != j && arr[i][j] != 0)) {
isIdentity = 0;
break;
}
}
}
if (isIdentity)
printf("The matrix is an Identity Matrix\n");
else
printf("The matrix is not an Identity Matrix\n");
return 0;
}
Output:
The matrix is an Identity Matrix