Check Identity Matrix

Check Identity Matrix” is a problem of matrix, where we need to check whether given matrix is an identity matrix or not.

There are basically three different types of matrix:

  1. Identity Matrix: An identity matrix is a square matrix of size N*N which have all the diagonal element as 1 and others as 0.
  2. Idempotent Matrix: An Idempotent matrix is a matrix which is when multiplied by itself produces the same matrix.
  3. Involuntary Matrix: An involutory matrix is a matrix which when multiplied by itself gives identity matrix.

Here, we are given a square matrix of size N*N. Our task is to check whether the given matrix is identity matrix or not. 

Example:

INPUT:
1 0 0 0 
0 1 0 0
0 0 1 0 
0 0 0 1
OUTPUT:
TRUE

C++ Program to check Identity Matrix is as follows:

/* Program to check Identity Matrix */
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    int N;  
    // Scan Dimensions of the matrix  
    cout<<"Enter the dimension of the matrix:\n";  
    cin>>N;  
      
    int matrix[N][N];  
      
    // Scan Matrix Elements  
    cout<<"Enter the elements of the matrix:\n";  
    for(int i = 0 ; i < N ; i++)  
    for(int j = 0 ; j < N ; j++)  
    cin>>matrix[i][j];  
      
    bool result = true;  
      
    // Check Identity matrix  
    for(int i = 0 ; i < N ; i++)  
    {  
        for(int j = 0 ; j < N ; j++)  
        {  
            if(i==j && matrix[i][j]!=1)  
            {  
                result = false;  
                break;  
            }  
            else if(i!=j && matrix[i][j]!=0)  
            {  
                result = false;  
                break;  
            }  
        }  
        if(!result)  
        break;  
    }  
      
    // Print the result  
    if(result)  
    cout<<"The Matrix is Identity Matrix";  
    else  
    cout<<"The Matrix is not Identity Matrix";  
}  

OUTPUT:
Enter the dimension of the matrix: 3
Enter the elements of the matrix:
1 0 0 
0 1 0
0 0 1
The Matrix is Identity Matrix

Related Posts:

  1. Program to check Idempotent Matrix.
  2. Program to check Involuntary Matrix.
  3. Program to print matrix in zig-zag order.
  4. Program to print matrix in spiral order.
  5. Program to sort a matrix.
  6. Program to Add two Matrix.
  7. Program to Transpose a Matrix.
  8. Program to Multiply Two Matrix.
  9. Program to count total number of words in a string.
  10. Program to find smallest and largest word of the String.
  11. Program to find Most Frequent Character of the String.
  12. Program to Remove all the blank spaces from the String.
  13. Program to check if String is isogram or not.
  14. Program to Reverse Each word of the String.
  15. Program to Print All the Substring of the Given String.
  16. Program to find longest palindromic Substring.
  17. Program to check Anagram Strings.
  18. Program to check whether two Strings are Rotation of each other or not.
  19. Swap Adjacent Elements of the Linked List.
  20. Count All Occurrences of a Particular Node in a Linked List.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *