Write a Go program to check whether the given matrix is symmetric or not. Any square matrix which remains the same after transposing is called the symmetric matrix. In this Go example, we transpose the given matrix and then compare each item in the original matrix against the transposed matrix. If they are equal, it is a symmetric matrix.
package main import "fmt" func main() { var i, j, rows, columns int var symmMat [10][10]int var transMat [10][10]int fmt.Print("Enter the Matrix rows and Columns = ") fmt.Scan(&rows, &columns) fmt.Println("Enter Matrix Items to Transpose = ") for i = 0; i < rows; i++ { for j = 0; j < columns; j++ { fmt.Scan(&symmMat[i][j]) } } for i = 0; i < rows; i++ { for j = 0; j < columns; j++ { transMat[j][i] = symmMat[i][j] } } count := 1 for i = 0; i < columns; i++ { for j = 0; j < rows; j++ { if symmMat[i][j] != transMat[i][j] { count++ break } } } if count == 1 { fmt.Println("This Matrix is a Symmetric Matrix") } else { fmt.Println("The Matrix is Not a Symmetric Matrix") } }
Enter the Matrix rows and Columns = 2 2
Enter Matrix Items to Transpose =
1 2
2 1
This Matrix is a Symmetric Matrix
Golang Program to Check the Matrix is a Symmetric Matrix using For loop range.
package main import "fmt" func main() { var symmMat [3][3]int var transMat [3][3]int fmt.Println("Enter Matrix Items to Transpose = ") for i, rows := range symmMat { for j := range rows { fmt.Scan(&symmMat[i][j]) } } for i, rows := range symmMat { for j := range rows { transMat[j][i] = symmMat[i][j] } } count := 1 for i, rows := range symmMat { for j := range rows { if symmMat[i][j] != transMat[i][j] { count++ break } } } if count == 1 { fmt.Println("This Matrix is a Symmetric Matrix") } else { fmt.Println("The Matrix is Not a Symmetric Matrix") } }
