Program to check Identity Matrix (original) (raw)

Given a **square matrix mat[][] of order **n*n, the task is to check if it is an **Identity Matrix.

**Identity Matrix: A square matrix is said to be an identity matrix if the elements of **main **diagonal are **one and all other elements are **zero. The identity Matrix is also known asthe **Unit Matrix.

**Input: mat[][] = [[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]

**Output: True
**Explanation: Main Diagonal = [1, 1, 1, 1], elements of main diagonal are one and all other elements are zero.

**Input: mat[][] = [[6, 10, 12, 0],
[0, 5, 0, 0],
[0, 0, 9, 0],
[0, 0, 0, 1]]

**Output: False
**Explanation: Main Diagonal = [6, 5, 9, 1], elements of main diagonal are not one.

C++ `

// C++ Program to check matrix is Identity matrix or not. #include <bits/stdc++.h> using namespace std;

// Function to check matrix // is Identity matrix or not. bool isIdentityMatrix(vector<vector> &mat) { int n = mat.size(); for (int i = 0; i < n; i++) {

    // condition to check main diagonal
    // elements are equal to 1 or not.
    if(mat[i][i] != 1)
        return false;

    for (int j = 0; j < n; j++) {

        // condition to check other elements 
        // except main diagonal are zero or not.
        if ((i != j) && (mat[i][j] != 0))
            return false;
        
    }
}
return true;

}

int main() { vector<vector> mat = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };

if (isIdentityMatrix(mat))
    cout << "True" << endl;
else
    cout << "False" << endl;
return 0;

}

Java

// Java Program to check matrix is Identity matrix or not. import java.util.*;

class GfG {

// Function to check matrix
// is Identity matrix or not.
static boolean isIdentityMatrix(int[][] mat) {
  
    int n = mat.length;
    for (int i = 0; i < n; i++) {

        // condition to check main diagonal
        // elements are equal to 1 or not.
        if(mat[i][i] != 1)
            return false;

        for (int j = 0; j < n; j++) {

            // condition to check other elements 
            // except main diagonal are zero or not.
            if ((i != j) && (mat[i][j] != 0))
                return false;
        }
    }

    return true;
}

public static void main(String[] args) {
    int[][] mat = {
        { 1, 0, 0, 0 },
        { 0, 1, 0, 0 },
        { 0, 0, 1, 0 },
        { 0, 0, 0, 1 }
    };

    if (isIdentityMatrix(mat))
        System.out.println("True");
    else
        System.out.println("False");
}

}

Python

Python Program to check if matrix

is Identity matrix or not.

def isIdentityMatrix(mat): n = len(mat) for i in range(0, n):

    # condition to check main diagonal
    # elements are equal to 1 or not.
    if (mat[i][i] != 1) :
        return False
    
    for j in range(0, n) :
        
        # condition to check other elements 
        # except main diagonal are zero or not.
        if ((i != j) and (mat[i][j] != 0)) :
            return False

return True

mat = [[ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ]]

if (isIdentityMatrix(mat)) : print("True") else : print("False")

C#

// C# Program to check matrix is // Identity matrix or not. using System;

class GfG {

// Function to check matrix
 // is Identity matrix or not.
static bool isIdentityMatrix(int [,]mat) {
  
      int n = mat.GetLength(0);
    for (int i = 0; i < n; i++) {

        // condition to check main diagonal
        // elements are equal to 1 or not.
        if(mat[i,i] != 1)
            return false;

        for (int j = 0; j < n; j++) {

            // condition to check other elements 
            // except main diagonal are zero or not.
            if ((i != j) && (mat[i,j] != 0))
                return false;
        }
    }

    return true;
}

static void Main() {
    int [,]mat = { { 1, 0, 0, 0 },
                    { 0, 1, 0, 0 },
                    { 0, 0, 1, 0 },
                    { 0, 0, 0, 1 } };

    if (isIdentityMatrix(mat))
        Console.WriteLine("True");
    else
        Console.WriteLine("False");
}

}

JavaScript

// JavaScript program to check matrix is // Identity matrix or not.

// Function to check matrix // is Identity matrix or not. function isIdentityMatrix(mat) {

let n = mat.length;
for (let i = 0; i < n; i++){

    // condition to check main diagonal
    // elements are equal to 1 or not.
    if(mat[i][i] != 1)
        return false;
    for (let j = 0; j < n; j++){

        // condition to check other elements 
        // except main diagonal are zero or not.
        if ((i != j) &&
            (mat[i][j] != 0))
            return false;
    }
}

return true;

}

//driver code let mat = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];

if (isIdentityMatrix(mat)) console.log("True"); else console.log("False");

`