0
0
CProgramBeginner · 2 min read

C Program to Check Identity Matrix with Output

A C program to check an identity matrix uses nested loops to verify that all diagonal elements are 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

Input3 1 0 0 0 1 0 0 0 1
OutputThe matrix is an identity matrix.
Input2 1 0 1 1
OutputThe matrix is not an identity matrix.
Input1 1
OutputThe matrix is an identity matrix.
🧠

How to Think About It

To check if a matrix is an identity matrix, look at each element. If the element is on the diagonal (where row and column numbers are equal), it must be 1. If it is off the diagonal, it must be 0. If any element breaks these rules, the matrix is not an identity matrix.
📐

Algorithm

1
Get the size of the square matrix.
2
Read the matrix elements.
3
For each element, check if it is on the diagonal.
4
If on diagonal, verify it is 1; if not, return false.
5
If off diagonal, verify it is 0; if not, return false.
6
If all checks pass, return true indicating identity matrix.
💻

Code

c
#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

1

Input matrix size

n = 3

2

Input matrix elements

matrix = [[1,0,0],[0,1,0],[0,0,1]]

3

Check element at (0,0)

i=0, j=0, diagonal, matrix[0][0]=1, condition true

4

Check element at (0,1)

i=0, j=1, off-diagonal, matrix[0][1]=0, condition true

5

Check element at (1,1)

i=1, j=1, diagonal, matrix[1][1]=1, condition true

6

Check element at (2,1)

i=2, j=1, off-diagonal, matrix[2][1]=0, condition true

7

All elements checked

No condition failed, return true

ijmatrix[i][j]CheckResult
001Diagonal == 1Pass
010Off-diagonal == 0Pass
111Diagonal == 1Pass
210Off-diagonal == 0Pass
💡

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

Using a single loop with early exit
c
#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;
}
This approach combines conditions in one if statement for concise code but is logically the same.
Check only diagonal and count zeros off-diagonal
c
#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;
}
This method counts zeros off-diagonal to verify completeness but adds extra counting overhead.

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.

ApproachTimeSpaceBest For
Separate if checksO(n^2)O(n^2)Clear logic, easy to understand
Combined condition ifO(n^2)O(n^2)Concise code, same performance
Counting zeros off-diagonalO(n^2)O(n^2)Extra validation but more complex
💡
Always check diagonal elements for 1 and off-diagonal elements for 0 to confirm identity matrix.
⚠️
Beginners often forget to check off-diagonal elements are zero, causing wrong results.