C PROGRAMMING PROGRAM • MATRICES
Check Whether a Matrix Is Symmetric
Learn how to check whether a matrix is symmetric using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
3 1 2 3 2 5 6 3 6 9
Sample output
Symmetric matrix
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int size, matrix[10][10], symmetric = 1;
scanf("%d", &size);
if (size < 1 || size > 10) return 1;
for (int row = 0; row < size; row++)
for (int column = 0; column < size; column++) scanf("%d", &matrix[row][column]);
for (int row = 0; row < size && symmetric; row++)
for (int column = row + 1; column < size; column++)
if (matrix[row][column] != matrix[column][row]) { symmetric = 0; break; }
printf(symmetric ? "Symmetric matrix\n" : "Not a symmetric matrix\n");
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Symmetric matrix
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Compare every element above the main diagonal with its reflected element below it.
- Display the computed result.
Compare every element above the main diagonal with its reflected element below it.
EFFICIENCY
Time and space complexity
Time complexity
O(rows × columns)
Auxiliary space
O(rows × columns)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
