Symmetric Matrix in C
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Check if Matrix is Symmetric in C: Program to Find Symmetric Matrix

šŸ“‘ On this page:
  • Introduction
  • C Program to Check Symmetric Matrix
  • Sample Output
  • Program Explanation
  • Algorithm
  • Practice Exercise
  • Frequently Asked Questions
šŸ“š In this tutorial, you will learn:
  • What is a symmetric matrix and its properties
  • How to check if a matrix is symmetric
  • How to use the transpose condition for verification
  • Step-by-step explanation of the program
  • Practice exercises to test your understanding

Introduction

In this tutorial, we will learn how to write a C program to check if a matrix is symmetric.

A symmetric matrix is a square matrix that is equal to its transpose. In other words, A[i][j] = A[j][i] for all i and j. This means the matrix is symmetric about its main diagonal.

šŸ’” Key Point: A symmetric matrix must be a square matrix (same number of rows and columns). Non-square matrices cannot be symmetric.

Symmetric matrices are important in many real-world applications, such as:

  • Linear Algebra: Eigenvalue problems, quadratic forms
  • Physics: Inertia tensors, stress tensors
  • Statistics: Covariance and correlation matrices
  • Computer Graphics: Rotation and transformation matrices

Visual Example

Symmetric Matrix (3Ɨ3):

[1  2  3]
[2  4  5]
[3  5  6]

Transpose of Symmetric Matrix:

[1  2  3]
[2  4  5]
[3  5  6]

āœ… Matrix = Transpose → Symmetric

Check: A[0][1] = 2, A[1][0] = 2 āœ“
A[0][2] = 3, A[2][0] = 3 āœ“
A[1][2] = 5, A[2][1] = 5 āœ“

āŒ Non-Symmetric Matrix:

[1  2  3]
[4  5  6]
[7  8  9]

Check: A[0][1] = 2, A[1][0] = 4 → Not equal!

C Program to Check if Matrix is Symmetric

#include <stdio.h>

int main() {
    int n, i, j;
    int symmetric = 1;  // Flag: 1 = symmetric, 0 = not symmetric
    
    // Ask user for matrix size
    printf("Enter the size of the square matrix: ");
    scanf("%d", &n);
    
    // Declare matrix
    int matrix[n][n];
    
    // Read elements into the matrix
    printf("\nEnter %d elements:\n", n * n);
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            printf("matrix[%d][%d] = ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }
    
    // Display the matrix
    printf("\n=== The Matrix ===\n");
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }
    
    // Check if matrix is symmetric
    // Only need to check upper triangle (i < j)
    for(i = 0; i < n; i++) {
        for(j = i + 1; j < n; j++) {
            if(matrix[i][j] != matrix[j][i]) {
                symmetric = 0;
                break;
            }
        }
        if(symmetric == 0) {
            break;
        }
    }
    
    // Display result
    printf("\n=== Result ===\n");
    if(symmetric == 1) {
        printf("āœ… The matrix is SYMMETRIC.\n");
        printf("(Matrix is equal to its transpose)\n");
    } else {
        printf("āŒ The matrix is NOT symmetric.\n");
        printf("(Matrix is not equal to its transpose)\n");
    }
    
    return 0;
}

Sample Output

Enter the size of the square matrix: 3

Enter 9 elements:
matrix[0][0] = 1
matrix[0][1] = 2
matrix[0][2] = 3
matrix[1][0] = 2
matrix[1][1] = 4
matrix[1][2] = 5
matrix[2][0] = 3
matrix[2][1] = 5
matrix[2][2] = 6

=== The Matrix ===
1	2	3
2	4	5
3	5	6

=== Result ===
āœ… The matrix is SYMMETRIC.
(Matrix is equal to its transpose)

When Matrix is Not Symmetric:

Enter the size of the square matrix: 3

Enter 9 elements:
matrix[0][0] = 1
matrix[0][1] = 2
matrix[0][2] = 3
matrix[1][0] = 4
matrix[1][1] = 5
matrix[1][2] = 6
matrix[2][0] = 7
matrix[2][1] = 8
matrix[2][2] = 9

=== The Matrix ===
1	2	3
4	5	6
7	8	9

=== Result ===
āŒ The matrix is NOT symmetric.
(Matrix is not equal to its transpose)

Example with Identity Matrix (Always Symmetric):

Enter the size of the square matrix: 4

Enter 16 elements:
matrix[0][0] = 1
matrix[0][1] = 0
matrix[0][2] = 0
matrix[0][3] = 0
matrix[1][0] = 0
matrix[1][1] = 1
matrix[1][2] = 0
matrix[1][3] = 0
matrix[2][0] = 0
matrix[2][1] = 0
matrix[2][2] = 1
matrix[2][3] = 0
matrix[3][0] = 0
matrix[3][1] = 0
matrix[3][2] = 0
matrix[3][3] = 1

=== The Matrix ===
1	0	0	0
0	1	0	0
0	0	1	0
0	0	0	1

=== Result ===
āœ… The matrix is SYMMETRIC.
(Matrix is equal to its transpose)

Program Explanation

Let's break down the code step by step:

  1. Include Header File: #include <stdio.h> includes the standard input/output library.
  2. Declare Variables:
    • int n; — size of the square matrix
    • int i, j; — loop counters
    • int symmetric = 1; — flag variable (1 = symmetric, 0 = not symmetric)
  3. Read Matrix: Reads n Ɨ n elements into the matrix.
  4. Check Symmetry:
    • Only the upper triangle (i < j) needs to be checked
    • Diagonal elements (i == j) are always equal to themselves
    • If matrix[i][j] != matrix[j][i], set symmetric = 0 and break
  5. Display Result: Prints whether the matrix is symmetric or not.
  6. Return: return 0; indicates successful program execution.

šŸ“ Note: We only need to check the upper triangle because symmetry is a symmetric property. If A[i][j] = A[j][i] for all i < j, then it automatically holds for all i > j as well.

Algorithm to Check Symmetric Matrix

Step-by-step algorithm:

  1. Start
  2. Read the size of the square matrix (n)
  3. Read n Ɨ n elements into the matrix
  4. Set symmetric = 1
  5. For i = 0 to n-1:
    • For j = i+1 to n-1:
      • If matrix[i][j] != matrix[j][i]:
        • Set symmetric = 0
        • Break
    • If symmetric == 0, break
  6. If symmetric == 1:
    • Print "Matrix is symmetric"
  7. Else:
    • Print "Matrix is not symmetric"
  8. End

Alternative Method: Using Transpose

Another way to check if a matrix is symmetric is to find its transpose and compare it with the original matrix:

#include <stdio.h>

int main() {
    int n, i, j;
    int symmetric = 1;
    
    printf("Enter the size of the square matrix: ");
    scanf("%d", &n);
    
    int matrix[n][n];
    int transpose[n][n];
    
    printf("\nEnter %d elements:\n", n * n);
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    
    // Find transpose
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }
    
    // Compare matrix with its transpose
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            if(matrix[i][j] != transpose[i][j]) {
                symmetric = 0;
                break;
            }
        }
        if(symmetric == 0) {
            break;
        }
    }
    
    printf("\n=== Matrix ===\n");
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }
    
    printf("\n=== Transpose ===\n");
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            printf("%d\t", transpose[i][j]);
        }
        printf("\n");
    }
    
    printf("\n=== Result ===\n");
    if(symmetric == 1) {
        printf("āœ… Matrix is SYMMETRIC (Matrix = Transpose)\n");
    } else {
        printf("āŒ Matrix is NOT symmetric (Matrix ≠ Transpose)\n");
    }
    
    return 0;
}

Properties of Symmetric Matrices

Property Description
Square Matrix Must have equal rows and columns
A = A^T Matrix equals its transpose
Diagonal Elements Any values (not required to be equal)
Off-Diagonal Elements A[i][j] = A[j][i] for all i ≠ j
Eigenvalues All eigenvalues are real
Eigenvectors Eigenvectors are mutually orthogonal

Time and Space Complexity

Method Time Complexity Space Complexity
Upper Triangle Check O(n²) O(1)
Transpose Method O(n²) O(n²)

šŸ’» Practice Exercise

Challenge 1: Write a program to check if a matrix is skew-symmetric (A[i][j] = -A[j][i]).

Challenge 2: Create a function that returns 1 if a matrix is symmetric and 0 otherwise.

šŸ” Click to Show Solution for Challenge 1
#include <stdio.h>

int main() {
    int n, i, j;
    int skew_symmetric = 1;
    
    printf("Enter the size of the square matrix: ");
    scanf("%d", &n);
    
    int matrix[n][n];
    
    printf("\nEnter %d elements:\n", n * n);
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    
    // Check if matrix is skew-symmetric
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            // Check condition: A[i][j] = -A[j][i]
            if(matrix[i][j] != -matrix[j][i]) {
                skew_symmetric = 0;
                break;
            }
        }
        if(skew_symmetric == 0) {
            break;
        }
    }
    
    printf("\n=== The Matrix ===\n");
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }
    
    printf("\n=== Result ===\n");
    if(skew_symmetric == 1) {
        printf("āœ… The matrix is SKEW-SYMMETRIC.\n");
    } else {
        printf("āŒ The matrix is NOT skew-symmetric.\n");
    }
    
    return 0;
}

Frequently Asked Questions

1. What is a symmetric matrix?

A symmetric matrix is a square matrix that is equal to its transpose. This means A[i][j] = A[j][i] for all elements.

2. What is the condition for a matrix to be symmetric?

The matrix must be square (same number of rows and columns) and A[i][j] = A[j][i] for all i and j.

3. Can a non-square matrix be symmetric?

No, a non-square matrix cannot be symmetric because transposing a non-square matrix changes its dimensions, so it cannot equal itself.

4. What is the time complexity of checking symmetry?

The time complexity is O(n²), where n is the size of the matrix. We need to check each element once.

5. What is the difference between symmetric and skew-symmetric?

Symmetric: A[i][j] = A[j][i]
Skew-Symmetric: A[i][j] = -A[j][i] and diagonal elements are zero.

šŸ’” Tip: When checking for symmetry, you only need to check the upper triangle (i < j) to save time. The diagonal and lower triangle are automatically verified.

šŸ“– Related Tutorials

  • Matrix Subtraction in C
  • Matrix Addition in C
  • Transpose a Matrix in C
  • More Array Assignments

Previous Topic: -->> Matrix Subtraction in C   ||   Next topic: -->> Sum of Each Row and Column


šŸ“š Explore More Topics

šŸ—„ļø SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

šŸ Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

ā˜• Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.