Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Program for Identity Matrix

  • Difficulty Level : Easy
  • Last Updated : 23 Apr, 2021

Introduction to Identity Matrix :

 The dictionary definition of an Identity Matrix is a square matrix in which all the elements of the principal or main diagonal are 1’s and all other elements are zeros. In the below image, every matrix is an Identity Matrix. 
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In linear algebra, this is sometimes called as a Unit Matrix, of a square matrix (size = n x n) with ones on the main diagonal and zeros elsewhere. The identity matrix is denoted by “ I “. Sometimes U or E is also used to denote an Identity Matrix. 
A property of the identity matrix is that it leaves a matrix unchanged if it is multiplied by an Identity Matrix.



Examples:  

Input  : 2
Output : 1 0
         0 1

Input :  4
Output : 1 0 0 0
         0 1 0 0
         0 0 1 0
         0 0 0 1
The explanation is simple. We need to make all
the elements of principal or main diagonal as 
1 and everything else as 0.

Program to print Identity Matrix : 
The logic is simple. You need to the print 1 in those positions where row is equal to column of a matrix and make all other positions as 0. 

C++




// C++ program to print Identity Matrix
#include<bits/stdc++.h>
using namespace std;
 
int Identity(int num)
{
    int row, col;
     
    for (row = 0; row < num; row++)
    {
        for (col = 0; col < num; col++)
        {
            // Checking if row is equal to column
            if (row == col)
                cout << 1 << " ";
            else
                cout << 0 << " ";
        }
        cout << endl;
    }
    return 0;
}
 
// Driver Code
int main()
{
    int size = 5;
    Identity(size);
    return 0;
}
 
// This code is contributed by shubhamsingh10

C




// C program to print Identity Matrix
#include<stdio.h>
 
int Identity(int num)
{
    int row, col;
     
    for (row = 0; row < num; row++)
    {
        for (col = 0; col < num; col++)
        {
            // Checking if row is equal to column
            if (row == col)
                printf("%d ", 1);
            else
                printf("%d ", 0);
        }
        printf("\n");
    }
    return 0;
}
 
// Driver Code
int main()
{
    int size = 5;
    identity(size);
    return 0;
}

Java




// Java program to print Identity Matrix
class GFG {
     
    static int identity(int num)
    {
        int row, col;
          
        for (row = 0; row < num; row++)
        {
            for (col = 0; col < num; col++)
            {
                // Checking if row is equal to column
                if (row == col)
                    System.out.print( 1+" ");
                else
                    System.out.print( 0+" ");
            }
            System.out.println();
        }
        return 0;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int size = 5;
        identity(size);
    }
}
 
/*This code is contributed by Nikita tiwari.*/

Python3




# Python code to print identity matrix
 
# Function to print identity matrix
def Identity(size):
    for row in range(0, size):
        for col in range(0, size):
 
            # Here end is used to stay in same line
            if (row == col):
                print("1 ", end=" ")
            else:
                print("0 ", end=" ")
        print()
 
# Driver Code       
size = 5
Identity(size)

C#




// C# program to print Identity Matrix
using System;
 
class GFG {
     
    static int identity(int num)
    {
        int row, col;
         
        for (row = 0; row < num; row++)
        {
            for (col = 0; col < num; col++)
            {
                // Checking if row is equal to column
                if (row == col)
                    Console.Write( 1+" ");
                else
                    Console.Write( 0+" ");
            }
            Console.WriteLine();
        }
        return 0;
    }
     
    // Driver Code
    public static void Main()
    {
        int size = 5;
        identity(size);
    }
}
 
/*This code is contributed by vt_m.*/

PHP




<?php
// PHP program to print
// Identity Matrix
 
function Identity($num)
{
    $row; $col;
     
    for ($row = 0; $row < $num; $row++)
    {
        for ($col = 0; $col < $num; $col++)
        {
             
            // Checking if row is
            // equal to column
            if ($row == $col)
            echo 1," ";
            else
            echo 0," ";
        }
        echo"\n";
    }
    return 0;
}
 
    // Driver Code
    $size = 5;
    identity($size);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
 
// Program to print Identity Matrix
   function Identity(num)
{
    var row;
    var col;
       
    for (row = 0; row < num; row++)
    {
        for (col = 0; col < num; col++)
        {
            // Checking if row is equal to column
            if (row == col)
                document.write( 1 + " ");
            else
                document.write( 0 + " ");
        }
        document.write(" \n" + "<br>");
    }
    return 0;
}
   
// Driver Code
     size = 5;
    Identity(size);
     
    //This code is contributed by simranarora5sos
     
</script>

Output: 

1  0  0  0  0  
0  1  0  0  0  
0  0  1  0  0  
0  0  0  1  0  
0  0  0  0  1  

 
Program to check if a given square matrix is Identity Matrix : 

C++




// CPP program to check if a given matrix is identity
#include<iostream>
using namespace std;
 
const int MAX = 100;
 
bool isIdentity(int mat[][MAX], int N)
{
    for (int row = 0; row < N; row++)
    {
        for (int col = 0; col < N; col++)
        {
            if (row == col && mat[row][col] != 1)
                return false;
            else if (row != col && mat[row][col] != 0)
                return false;
        }
    }
    return true;
}
 
// Driver Code
int main()
{
    int N = 4;
    int mat[][MAX] = {{1, 0, 0, 0},
                    {0, 1, 0, 0},
                    {0, 0, 1, 0},
                    {0, 0, 0, 1}};
    if (isIdentity(mat, N))
       cout << "Yes ";
    else
       cout << "No ";
    return 0;
}

Java




// Java program to check if a given
// matrix is identity
class GFG {
     
    int MAX = 100;
  
    static boolean isIdentity(int mat[][], int N)
    {
        for (int row = 0; row < N; row++)
        {
            for (int col = 0; col < N; col++)
            {
                if (row == col && mat[row][col] != 1)
                    return false;
                else if (row != col && mat[row][col] != 0)
                    return false;
            }
        }
        return true;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int N = 4;
        int mat[][] = {{1, 0, 0, 0},
                       {0, 1, 0, 0},
                       {0, 0, 1, 0},
                       {0, 0, 0, 1}};
         
        if (isIdentity(mat, N))
           System.out.println("Yes ");
        else
           System.out.println("No ");
    }
}
 
 
/*This code is contributed by Nikita Tiwari.*/

Python3




# Python3 program to check
# if a given matrix is identity
MAX = 100;
def isIdentity(mat, N):
    for row in range(N):
        for col in range(N):
            if (row == col and
                mat[row][col] != 1):
                return False;
            elif (row != col and
                  mat[row][col] != 0):
                return False;
    return True;
 
# Driver Code
N = 4;
mat = [[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]];
if (isIdentity(mat, N)):
    print("Yes ");
else:
    print("No ");
 
# This code is contributed
# by mits

C#




// C# program to check if a given
// matrix is identity
using System;
 
class GFG {
     
    //int MAX = 100;
 
    static bool isIdentity(int[,] mat, int N)
    {
        for (int row = 0; row < N; row++)
        {
            for (int col = 0; col < N; col++)
            {
                if (row == col && mat[row,col] != 1)
                    return false;
                else if (row != col && mat[row,col] != 0)
                    return false;
            }
        }
        return true;
    }
     
    // Driver Code
    public static void Main()
    {
        int N = 4;
        int [,]mat = {{1, 0, 0, 0},
                      {0, 1, 0, 0},
                      {0, 0, 1, 0},
                      {0, 0, 0, 1}};
         
        if (isIdentity(mat, N))
        Console.WriteLine("Yes ");
        else
        Console.WriteLine("No ");
    }
}
 
 
/*This code is contributed by vt_m.*/

PHP




<?php
// PHP program to check if a
// given matrix is identity
// $MAX = 100;
 
function isIdentity($mat, $N)
{
    for ($row = 0; $row < $N; $row++)
    {
        for ( $col = 0; $col < $N; $col++)
        {
            if ($row == $col and
                $mat[$row][$col] != 1)
                return false;
            else if ($row != $col &&
                     $mat[$row][$col] != 0)
                return false;
        }
    }
    return true;
}
 
    // Driver Code
    $N = 4;
    $mat = array(array(1, 0, 0, 0),
                 array(0, 1, 0, 0),
                 array(0, 0, 1, 0),
                 array(0, 0, 0, 1));
    if (isIdentity($mat, $N))
    echo "Yes ";
    else
    echo "No ";
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
 
// JavaScript program to check if a given
// matrix is identity
let MAX = 100;
 
function isIdentity(mat, N)
{
    for(let row = 0; row < N; row++)
    {
        for(let col = 0; col < N; col++)
        {
            if (row == col && mat[row][col] != 1)
                return false;
            else if (row != col && mat[row][col] != 0)
                return false;
        }
    }
    return true;
}
 
// Driver Code
let N = 4;
let mat = [ [ 1, 0, 0, 0 ],
            [ 0, 1, 0, 0 ],
            [ 0, 0, 1, 0 ],
            [ 0, 0, 0, 1 ] ];
  
if (isIdentity(mat, N))
    document.write("Yes ");
else
    document.write("No ");
     
// This code is contributed by sanjoy_62
 
</script>

Output:

Yes

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!