C Introduction

Control Statement

Functions

Array

Pointers

Structure and Union

No Examples found for this topic - CodeHelpPro

Data Files

No Examples found for this topic - CodeHelpPro

# Program to check square matrix is symmetric or not

In this example, We will learn a program to check square matrix is symmetric or not. Before that, You must have knowledge of the following topics.

- For Loop
- Array

Few important notes you must know:

- A square matrix will be symmetric if transpose of that matrix will be equal to original matrix.

```
1 3 7
3 5 9
7 9 2
```

Above matrix is square matrix and symmetric matrix.

- We can find transpose of the matrix by exchanging row and column indices.
- We can find transpose only of the square matrix.

## Algorithm

This is the algorithm of this program

Step 1: Start Step 2: Declare variables c, d, a, b, n and temp Step 3: Take input from user and store in n Step 4: Assign c = 0 Step 5: REPEAT this step until c < n Assign d = 0 REPEAT this step until d < n Take input from user and store in a[c][d] Increase d by 1 Increase c by 1 Step 6: Assign c = 0 Step 7: REPEAT this step until c < n Assign d = 0 REPEAT this step until d < n b[d][c] = a[c][d] Increase d by 1 Increase c by 1 Step 8: Declare isSymmetric = 0 and Assign c = 0 Step 9: REPEAT this step until c < n Assign d = 0 IF isSymmetric == 0 REPEAT this step until d < n IF a[c][d] != b[c][d] isSymmetric = 1 break; Increase d by 1 Increase c by 1 Step 10: IF isSymmetric == 0 Print \"Matrix is symmetric\" ELSE Print \"Matrix is not symmetric\" Step 11: End

## Flowchart

This is the flowchart of this program

## CODE: Check Square Matrix is symmetric or not

Now, We will see the code to sort array elements using for loop.

```
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, d, a[10][10], b[10][10], n, temp;
printf("\nEnter the dimension of the matrix: \n\n");
scanf("%d", &n);
printf("\nEnter the %d elements of the matrix: \n\n", n * n);
for (c = 0; c < n; c++)
for (d = 0; d < n; d++)
scanf("%d", &a[c][d]);
// finding transpose of a matrix and storing it in b[][]
for (c = 0; c < n; c++)
for (d = 0; d < n; d++)
b[d][c] = a[c][d];
// printing the original matrix
printf("\n\nThe original matrix is: \n\n");
for (c = 0; c < n; c++)
{
for (d = 0; d < n; d++)
{
printf("%d\t", a[c][d]);
}
printf("\n");
}
// printing the transpose of the entered matrix
printf("\n\nThe Transpose matrix is: \n\n");
for (c = 0; c < n; c++)
{
for (d = 0; d < n; d++)
{
printf("%d\t", b[c][d]);
}
printf("\n");
}
// checking if the original matrix is same as its transpose
for (c = 0; c < n; c++)
{
for (d = 0; d < n; d++)
{
/*
if they differ by a single element,
the matrix is not symmetric
*/
if (a[c][d] != b[c][d])
{
printf("\n\nMatrix is not Symmetric\n\n");
exit(0);
}
}
}
printf("\n\nMatrix is Symmetric\n\n");
return 0;
}
```

The output of above program is

```
Enter the dimension of the matrix:
2
Enter the 4 elements of the matrix:
1
2
3
4
The original matrix is:
1 2
3 4
```

Subscribe

Login

0 Comments

Inline Feedbacks

View all comments