C Program to Check if a Matrix is Symmetric

Introduction

A square matrix is said to be symmetric if it is equal to its transpose. In other words, for a matrix to be symmetric, the element at position (i, j) must be equal to the element at position (j, i) for all i and j. This guide will show you how to write a C program to check if a given square matrix is symmetric.

Problem Statement

Create a C program that:

  • Takes a square matrix as input from the user.
  • Checks whether the matrix is symmetric.
  • Displays whether the matrix is symmetric or not.

Example:

  • Input:

    • Matrix:
      1 2 3
      2 4 5
      3 5 6
      
  • Output: The matrix is symmetric.

  • Input:

    • Matrix:
      1 2 3
      4 5 6
      7 8 9
      
  • Output: The matrix is not symmetric.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the matrix, its dimensions, and loop counters.
  4. Input the Dimensions and Elements of the Matrix: Use loops to take input from the user for the matrix.
  5. Check if the Matrix is Symmetric: Compare each element with its corresponding element in the transpose. If any element does not match, the matrix is not symmetric.
  6. Display the Result: Use printf to display whether the matrix is symmetric or not.

C Program to Check if a Matrix is Symmetric

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the matrix and dimensions
    int n;
    int matrix[100][100];
    int i, j;
    int isSymmetric = 1;  // Assume the matrix is symmetric initially

    // Step 2: Prompt the user to enter the dimension of the square matrix
    printf("Enter the dimension of the square matrix (n x n): ");
    scanf("%d", &n);

    // Step 3: Input the elements of the matrix
    printf("Enter elements of the matrix:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Step 4: Check if the matrix is symmetric
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            if (matrix[i][j] != matrix[j][i]) {
                isSymmetric = 0;  // Set to 0 if any element does not match
                break;
            }
        }
        if (!isSymmetric) {
            break;
        }
    }

    // Step 5: Display the result
    if (isSymmetric) {
        printf("The matrix is symmetric.\n");
    } else {
        printf("The matrix is not symmetric.\n");
    }

    return 0;  // Step 6: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variable n stores the dimension of the square matrix. The matrix array stores the elements of the matrix. The variable isSymmetric is a flag initially set to 1 (true) and is used to check if the matrix is symmetric. The variables i and j are used as loop counters.

Step 2: Input the Dimension of the Matrix

  • The program prompts the user to enter the dimension (n x n) of the square matrix using scanf.

Step 3: Input the Elements of the Matrix

  • The program uses nested for loops to take input for each element of the matrix from the user.

Step 4: Check if the Matrix is Symmetric

  • The program uses nested for loops to compare each element with its corresponding element in the transpose:
    • If matrix[i][j] is not equal to matrix[j][i], the matrix is not symmetric, and isSymmetric is set to 0 (false).

Step 5: Display the Result

  • The program checks the value of isSymmetric:
    • If it is 1, the matrix is symmetric.
    • If it is 0, the matrix is not symmetric.

Step 6: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter the dimension of the square matrix (n x n): 3
Enter elements of the matrix:
1 2 3
2 4 5
3 5 6
The matrix is symmetric.

Example 2:

Enter the dimension of the square matrix (n x n): 3
Enter elements of the matrix:
1 2 3
4 5 6
7 8 9
The matrix is not symmetric.

Conclusion

This C program demonstrates how to check if a square matrix is symmetric by comparing its elements with those in the transpose. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top