C++ Program to Check if a Matrix is an Identity Matrix

This is a C++ Program to Find if an Array is an Identity Matrix.

Problem Description

The program takes a array and checks if it is an identity matrix. An identity matrix is a square matrix with main diagonal elements as 1 and rest 0.

Problem Solution

1. The program takes the number of rows and columns of the matrix.
2. If they are not equal, it cannot be an identity matrix. The program is exited.
3. Else, the matrix is checked if only the main diagonal elements are 1 and rest are 0.
4. The result is printed.
5. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find if an Array is an Identity Matrix. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int i, j, m, n, A[10][10], flag = 0;
  6.     cout << "Enter number of rows and columns : ";
  7.     cin >> m >> n;
  8.     if (m != n)
  9.     {
  10.         cout << "Matrix is not a square matrix!";
  11.         exit(0);
  12.     }
  13.     cout << "Enter array elements : ";
  14.     for (i = 0; i < m; i++)
  15.         for (j = 0; j < n; j++)    
  16.             cin >> A[i][j];
  17.     for (i = 0; i < m; i++)
  18.         for (j = 0; j < n; j++)
  19.         {
  20.             if ((A[i][j] != 1) && (A[j][i] != 0))
  21.             {
  22.                 flag = 1;
  23.                 break;
  24.             }
  25.         }
  26.     if (flag == 0)
  27.         cout << "The given array is an identity matrix.\n ";
  28.     else
  29.         cout << "The given array is not an identity matrix.\n ";
  30.     for (i = 0; i < m; i++)
  31.     {
  32.         for (j = 0; j < n; j++)	
  33.             cout << A[i][j] << " ";
  34.         cout << "\n ";
  35.     }
  36.     return 0;
  37. }
Program Explanation

1. The user is asked to enter the number of rows and columns.
2. If they are not equal, the array is not a square matrix, hence it cannot be an identity matrix. The program is exited.
3. Else, the elements are asked to enter and stored in ‘A’.
4. If the diagonal elements of the matrix are not 1 and non diagonal elements are not 0, the variable ‘flag’ is assigned value 1.
5. If flag is equal to 0, the entered array is an identity matrix, else it is not.
6. The result is then printed.

advertisement
Runtime Test Cases
Case 1 :
Enter number of rows and columns : 2 2
Enter array elements : 1 2 3 4
The given array is not an identity matrix.
 1 2
 3 4
 
Case 2 :
Enter number of rows and columns : 3 2
Matrix is not a square matrix!
 
Case 3 :
Enter number of rows and columns : 3 3
Enter array elements : 1 0 0 0 1 0 0 0 1
The given array is an identity matrix.
 1 0 0
 0 1 0
 0 0 1

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

🔥 Enroll for Free Data Structure Certification - December 2025

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.