C Program to Check Identity Matrix with Output
1 and all off-diagonal elements are 0. For example, use if (matrix[i][j] != 1 && i == j) return false; and if (matrix[i][j] != 0 && i != j) return false; inside loops.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> #include <stdbool.h> bool isIdentityMatrix(int n, int matrix[n][n]) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j && matrix[i][j] != 1) return false; if (i != j && matrix[i][j] != 0) return false; } } return true; } int main() { int n; scanf("%d", &n); int matrix[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &matrix[i][j]); } } if (isIdentityMatrix(n, matrix)) printf("The matrix is an identity matrix.\n"); else printf("The matrix is not an identity matrix.\n"); return 0; }
Dry Run
Let's trace the example input 3x3 identity matrix through the code
Input matrix size
n = 3
Input matrix elements
matrix = [[1,0,0],[0,1,0],[0,0,1]]
Check element at (0,0)
i=0, j=0, diagonal, matrix[0][0]=1, condition true
Check element at (0,1)
i=0, j=1, off-diagonal, matrix[0][1]=0, condition true
Check element at (1,1)
i=1, j=1, diagonal, matrix[1][1]=1, condition true
Check element at (2,1)
i=2, j=1, off-diagonal, matrix[2][1]=0, condition true
All elements checked
No condition failed, return true
| i | j | matrix[i][j] | Check | Result |
|---|---|---|---|---|
| 0 | 0 | 1 | Diagonal == 1 | Pass |
| 0 | 1 | 0 | Off-diagonal == 0 | Pass |
| 1 | 1 | 1 | Diagonal == 1 | Pass |
| 2 | 1 | 0 | Off-diagonal == 0 | Pass |
Why This Works
Step 1: Check diagonal elements
Diagonal elements must be exactly 1 to satisfy identity matrix rules.
Step 2: Check off-diagonal elements
All elements not on the diagonal must be 0 to keep identity matrix property.
Step 3: Return result
If any element breaks the rules, return false; otherwise, return true.
Alternative Approaches
#include <stdio.h> #include <stdbool.h> bool isIdentity(int n, int matrix[n][n]) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if ((i == j && matrix[i][j] != 1) || (i != j && matrix[i][j] != 0)) return false; } } return true; } int main() { int n; scanf("%d", &n); int matrix[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &matrix[i][j]); if (isIdentity(n, matrix)) printf("The matrix is an identity matrix.\n"); else printf("The matrix is not an identity matrix.\n"); return 0; }
#include <stdio.h> #include <stdbool.h> bool checkIdentity(int n, int matrix[n][n]) { int zeroCount = 0; for (int i = 0; i < n; i++) { if (matrix[i][i] != 1) return false; for (int j = 0; j < n; j++) { if (i != j && matrix[i][j] == 0) zeroCount++; else if (i != j && matrix[i][j] != 0) return false; } } return zeroCount == n*(n-1); } int main() { int n; scanf("%d", &n); int matrix[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &matrix[i][j]); if (checkIdentity(n, matrix)) printf("The matrix is an identity matrix.\n"); else printf("The matrix is not an identity matrix.\n"); return 0; }
Complexity: O(n^2) time, O(n^2) space
Time Complexity
The program uses nested loops to check each element once, resulting in O(n^2) time for an n x n matrix.
Space Complexity
The matrix is stored in a 2D array of size n x n, so space complexity is O(n^2). No extra significant space is used.
Which Approach is Fastest?
All approaches check every element once, so time complexity is similar. The combined condition approach is slightly more concise.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Separate if checks | O(n^2) | O(n^2) | Clear logic, easy to understand |
| Combined condition if | O(n^2) | O(n^2) | Concise code, same performance |
| Counting zeros off-diagonal | O(n^2) | O(n^2) | Extra validation but more complex |