# C++ program to check whether the matrix is an identity matrix or not using class

Submitted by Shubh Pachori, on September 15, 2022

## Problem statement

Given a matrix of integers, we to check whether it is an identity matrix or not using the class and object approach.

Example:

```Input:
Enter Matrix elements :
[0][0]: 1
[0][1]: 2
[0][2]: 3
[1][0]: 4
[1][1]: 5
[1][2]: 6
[2][0]: 7
[2][1]: 8
[2][2]: 9

Output:
Entered Matrix :
1  2  3
4  5  6
7  8  9

The matrix is not an identity matrix!
```

## C++ code to check whether the matrix is an identity matrix or not using the class and object approach

```#include <iostream>
using namespace std;

// create a class
class Matrix {
// private data member
private:
int matrix[3][3];

// public member funtions
public:
// getMatrix() funtion to insert matrix
void getMatrix() {
cout << "Enter Matrix elements :" << endl;
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
cout << "[" << row << "][" << column << "]: ";
cin >> matrix[row][column];
}
}
}

// identityMatrix() funtion to check whether the
// matrix is a identity matrix or not
void identityMatrix() {
// initialising int type variables
// to perform operations
int row, column, check = 1;

cout << "\nEntered Matrix :" << endl;

// for loop to show the inserted matrix
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
cout << matrix[row][column] << "  ";
}
cout << "\n";
}

// nested for loop to traverse the whole matrix
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
// if condition to check for the one's and zero's in the matrix
if (matrix[row][column] != 1 && matrix[row][column] != 0) {
check = 0;
break;
}
}
}

if (check == 1) {
cout << "\nThe matrix is an identity matrix!" << endl;
} else {
cout << "\nThe matrix is not an identity matrix!" << endl;
}
}
};

int main() {
// create an object
Matrix M;

// calling getMatrix() funtion to
// insert Matrix
M.getMatrix();

// calling identityMatrix() funtion to check
// the matrix whether it is an identity
// matrix or not
M.identityMatrix();

return 0;
}
```

### Output

```Enter Matrix elements :
[0][0]:1
[0][1]:0
[0][2]:0
[1][0]:0
[1][1]:1
[1][2]:0
[2][0]:0
[2][1]:0
[2][2]:1

Entered Matrix :
1  0  0
0  1  0
0  0  1

The matrix is an identity matrix!
```

### Explanation

In the above code, we have created a class Matrix, one int type 2d array data members matrix[3][3] to store the elements of the matrix, and public member functions getMatrix() and identityMatrix() to store the matrix elements and to check whether the matrix is a identity matrix or not.

In the main() function, we are creating an object M of class Matrix, reading the inputted matrix by the user using getMatrix() function, and finally calling the identityMatrix() member function to check whether the matrix is a identity matrix or not. The identityMatrix() function contains the logic to check whether the matrix is a identity matrix or not and printing the result.