In Go programming, we can use the equals operator to check the two matrixes are equal or not. This Go program allows users to enter the two matrixes, size, and items. Next, we used the if else statement (if firstMat == secondMat) to check whether they are equal or not.
package main import "fmt" func main() { var i, j, rows, columns int var firstMat [10][10]int var secondMat [10][10]int fmt.Print("Enter the Matrix rows and Columns = ") fmt.Scan(&rows, &columns) fmt.Println("Enter the First Matrix Items = ") for i = 0; i < rows; i++ { for j = 0; j < columns; j++ { fmt.Scan(&firstMat[i][j]) } } fmt.Println("Enter the Second Matrix Items = ") for i = 0; i < rows; i++ { for j = 0; j < columns; j++ { fmt.Scan(&secondMat[i][j]) } } if firstMat == secondMat { fmt.Println("The First Matrix and the Second Matrix are Equal") } else { fmt.Println("The First Matrix is Not Equal to the Second Matrix") } }
SureshMac:GoExamples suresh$ go run matEqual0.go
Enter the Matrix rows and Columns = 2 2
Enter the First Matrix Items =
1 2
3 4
Enter the Second Matrix Items =
4 5
6 7
The First Matrix is Not Equal to the Second Matrix
SureshMac:GoExamples suresh$ go run matEqual0.go
Enter the Matrix rows and Columns = 2 2
Enter the First Matrix Items =
1 2
3 4
Enter the Second Matrix Items =
1 2
3 4
The First Matrix and the Second Matrix are Equal
Golang Program to Check Two Matrixes are Equal.
In this Go example, the nested for loop iterate the matrix items. Within the loop, the if statement (if firstMat[i][j] != secondMat[i][j]) checks each item of the first matrix is not equal to the second matrix. If True, assign zero to the isequal variable. Finally, we used if-else to check matrices are equal or not.
package main import "fmt" func main() { var i, j, rows, columns int var firstMat [10][10]int var secondMat [10][10]int fmt.Print("Enter the Matrix rows and Columns = ") fmt.Scan(&rows, &columns) fmt.Println("Enter the First Matrix Items = ") for i = 0; i < rows; i++ { for j = 0; j < columns; j++ { fmt.Scan(&firstMat[i][j]) } } fmt.Println("Enter the Second Matrix Items = ") for i = 0; i < rows; i++ { for j = 0; j < columns; j++ { fmt.Scan(&secondMat[i][j]) } } isequal := 1 for i = 0; i < rows; i++ { for j = 0; j < columns; j++ { if firstMat[i][j] != secondMat[i][j] { isequal = 0 break } } } if isequal == 1 { fmt.Println("The First Matrix and the Second Matrix are Equal") } else { fmt.Println("The First Matrix is Not Equal to the Second Matrix") } }
Enter the Matrix rows and Columns = 2 3
Enter the First Matrix Items =
1 2 3
4 5 6
Enter the Second Matrix Items =
1 2 3
4 5 6
The First Matrix and the Second Matrix are Equal
Go Program to find the two Matrixes are Equal or not using for loop range.
package main import "fmt" func main() { var firstMat [3][3]int var secondMat [3][3]int fmt.Println("Enter the First Matrix Items = ") for i, rows := range firstMat { for j := range rows { fmt.Scan(&firstMat[i][j]) } } fmt.Println("Enter the Second Matrix Items = ") for i, rows := range secondMat { for j := range rows { fmt.Scan(&secondMat[i][j]) } } isequal := 1 for i, rows := range firstMat { for j := range rows { if firstMat[i][j] != secondMat[i][j] { isequal = 0 break } } } if isequal == 1 { fmt.Println("The First Matrix and the Second Matrix are Equal") } else { fmt.Println("The First Matrix is Not Equal to the Second Matrix") } }