# Check if a Matrix is Symmetric or not in C++

Here we’ll try to check if a matrix, taken from the user, fulfills the conditions to be a symmetric matrix or not in C++.

## What is a symmetric matrix?

A matrix is said to be symmetric when its rotation about the principle diagonal (transpose) results in the original matrix itself.

## Can any matrix be symmetric?

NO. Only matrices that satisfy the following conditions can be symmetric :

1. The matrix is a square matrix, i.e., the number of rows and columns are equal.
2. The transpose of the matrix results in the original matrix.

## A simple code to check if a matrix is symmetric in C++

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

int main()
{
//variables used
int r,c,i,j,flag=0;

//taking input for number of rows and columns
cout<<"Enter number of rows and columns :"<<endl;
cin>>r>>c;

//array of user dimensions
int arr[r][c];

//checking if the matrix is square
if(r!=c)
cout<<"\nNot a symmetric matrix";
else
{
//taking input for values of the matrix
cout<<"\nEnter the values in the matrix :"<<endl;
for(i=0 ; i<r ; i++)
{
for(j=0 ; j<c ; j++)
{
cin>>arr[i][j];
}
}

//printing the user entered matrix
cout<<"The entered matrix is :";
for(i=0 ; i<r ; i++)
{
cout<<endl;
for(j=0 ; j<c ; j++)
{
cout<<arr[i][j];
}
}

//checking for symmetry
for(i=0 ; i<r ; i++)
{
for(j=0 ; j<c ; j++)
{
if (arr[i][j] != arr[j][i])
{
flag=1;
break;
}
}
}

//displaying the result
if(flag == 0)
cout<<"\nIt's a symmetric matrix";
else
cout<<"\nNot a symmetric matrix";
}
return 0;
}
```

#### Output

CASE 1 – Matrix is not square

```Enter number of rows and columns :
2
3
Not a symmetric matrix```

As the number of rows and columns is different the matrix can not be a square matrix. Which is one of the two conditions that have to be fulfilled for being symmetric matrices.

CASE 2 – Matrix is square but not symmetric

```Enter number of rows and columns :
3
3

Enter the values in the matrix :
1
2
3
4
5
6
7
8
9
The entered matrix is :
123
456
789
Not a symmetric matrix```

Here the number of rows and columns are the same so the first condition is fulfilled of being a square matrix. But since arr[i][j] is not equal to arr[j][i] for all values of i and j, this matrix will not result in itself after taking its transpose. Hence, it is not symmetric.

CASE 3 – Matrix is square and symmetric

```Enter number of rows and columns :
3
3

Enter the values in the matrix :
0
1
1
1
0
1
1
1
0
The entered matrix is :
011
101
110
It's a symmetric matrix```

As we can see, in this example, the number of rows and columns are the same. Therefore this is a square matrix and also the first condition is fulfilled. Also here, arr[i][j] is equal to arr[j][i] for all values of i and j, hence, this matrix will result in itself after taking its transpose. So it is symmetric.

In the above code, first, we take integer values for rows and columns. Then we check if the matrix is square or not. If the matrix is square we proceed towards taking the values of the matrix from the user. Afterward, we display the user’s matrix and check for symmetry by iterating for each element of the matrix. If at any point an element does not yield symmetry the flag is assigned the value of 1. In the end, we print the result per the value of the flag.

You can also read: Check Whether a Matrix is Diagonal or not in C++