Write a program that reads a square matrix and checks whether it is symmetric.
A square matrix is called symmetric if for all values of i and j, a[i][j]=a[j][i].
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
#define SIZE 50
void main()
{
int A[SIZE][SIZE];
int R; /* dimensions */
int I, J; /* counters */
int symmetric;
do {
printf("Number of rows and columns of the square matrix (max.%d) : ", SIZE);
scanf("%d", &R);
} while (R <= 0 || R > SIZE);
for (I=0; I<R; I++)
for (J=0; J<R; J++)
{
printf("A[%d][%d] : ",I,J);
scanf("%d", &A[I][J]);
}
printf("\nMatrix :\n");
for (I=0; I<R; I++)
{
for (J=0; J<R; J++)
printf("%7d", A[I][J]);
printf("\n");
}
symmetric=1;
for (I=0; I<R && symmetric==1; I++)
for (J=0; J<I && symmetric==1; J++)
if(A[I][J]!=A[J][I])
symmetric=0;
if(symmetric==1)
printf("Matrix is symmetric\n");
else
printf("Matrix is NOT symmetric\n");
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Print odd numbers in a range of integers using recursion