---
title: "Write a Rust program to determine if a given matrix is symmetric or not."  
description: "Write a Rust program to determine if a given matrix is symmetric or not."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158836/write-a-rust-program-to-determine-if-a-given-matrix-is-symmetric-or-not  
category: "rust"  
tags: ["programs", "rust"]  
reading_time: 2 minutes  

---

# Write a Rust program to determine if a given matrix is symmetric or not.

Write a [Rust](https://www.mindstick.com/forum/160170/explain-rust-s-ownership-borrowing-and-lifetimes-system) [program to determine](https://www.mindstick.com/forum/158797/create-a-rust-program-to-determine-if-a-number-is-prime-or-not) if a given [matrix](https://www.mindstick.com/forum/2412/how-to-convert-matrix-to-uiimage) is symmetric or not.

## Replies

### Reply by Aryan Kumar

Sure, here is a [Rust program](https://www.mindstick.com/forum/158800/create-a-rust-program-to-find-the-gcd-greatest-common-divisor-of-two-numbers) to determine if a given matrix is symmetric or not:

Rust

```plaintext
fn is_symmetric(matrix: &[[i32; 4]; 4]) -> bool {
    for i in 0..4 {
        for j in 0..4 {
            if matrix[i][j] != matrix[j][i] {
                return false;
            }
        }
    }

    return true;
}

fn main() {
    let matrix = [[1, 2, 3, 4], [2, 5, 6, 7], [3, 6, 9, 8], [4, 7, 8, 10]];
    let is_symmetric = is_symmetric(&matrix);
    println!("The matrix is {}symmetric", if is_symmetric { "" } else { "not " });
}
```

This program first defines a function called is_symmetric that takes a 4x4 matrix of i32 numbers as input and returns a bool value. The function iterates through the matrix, comparing each element to its corresponding element on the other side of the main diagonal. If any of the elements are not equal, then the function returns false. Otherwise, the function returns true.

The main function of the program then calls the is_symmetric function with the matrix [[1, 2, 3, 4], [2, 5, 6, 7], [3, 6, 9, 8], [4, 7, 8, 10]] as input. The result of the function call is then printed to the console.

To run the program, you can save it as a file called is_symmetric.rs and then compile it using the following command:

Code snippet

```plaintext
rustc is_symmetric.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./is_symmetric
```

This will print the following output to the console:

Code snippet

```plaintext
The matrix is symmetric
```


---

Original Source: https://www.mindstick.com/forum/158836/write-a-rust-program-to-determine-if-a-given-matrix-is-symmetric-or-not

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
