GO Program to check symmetric matrix

How to check the symmetric matrix

Here we are explaining how to check the symmetric matrix in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package and it helps to write standard output. In order to use these functions, we need to import the “fmt” package.

for initialization; condition; post{
       // statements....
} 

The initialization statement is executed before for loop starts and it is optional. The condition statement has a boolean expression. The post statement is executed after the body of the for-loop.

 

How to check the symmetric matrix in GO Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to check the symmetric matrix in the Go language. Here variable mat,TMat is used to hold the matrix elements and iteration elements i,j. Read the number of rows and columns for reading matrix elements into the variables row, col. After reading, elements find the transpose of a matrix using two nested for loops. Compare each element of the transpose matrix is equal to the original matrix. If equal it is a symmetric matrix.  Finally, print the result. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Declare the matrix variables mat,TMat, and iteration variables i,j

STEP 4: Read the number of rows and columns into row, col

STEP 5: Read the matrix for finding the transpose

STEP 6: Find the transpose of a matrix using two nested for loops

STEP 7: Compare each element of the transpose matrix with the original matrix

STEP 8: If equal print the message as a symmetric matrix using fmt.print()

 

GO Source Code

                                          package main

import "fmt"

func main() {
    var i, j, row, col int

    var mat [10][10]int
    var TMat [10][10]int

    fmt.Print("Enter the number of rows and columns = ")
    fmt.Scan(&row;, &col;)

    fmt.Println("Enter matrix items = ")
    for i = 0; i < row; i++ {
        for j = 0; j < col; j++ {
            fmt.Scan(&mat;[i][j])
        }
    }
    for i = 0; i < row; i++ {
        for j = 0; j < col; j++ {
            TMat[j][i] = mat[i][j]
        }
    }
    count := 1
    for i = 0; i < col; i++ {
        for j = 0; j < row; j++ {
            if mat[i][j] != TMat[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")
    }

}
                                      

OUTPUT

Enter the number of rows and columns = 2 2
Enter matrix items = 
2  3
3  2
This matrix is a Symmetric Matrix