
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Whether Two Matrices Are Equal in Go
In this tutorial, we will write a golang program to check whether two matrices are equal or not. A matrix is a collection of numbers arranged in rows and columns, a two-dimensional array.
Check Whether Two Matrices are Equal or Not using If Statment
In this example we will compare the equality of two matrices using if condition statements.
Algorithm
Step 1 ? Import the fmt package.
Step 2 ? Start the main() function.
Step 3 ? Initialize and assign values the two matrices.
Step 4 ? Print these matrices on the screen.
Step 5 ? Now use the equality(==) operator to check whether given matrices are equal or not.
Step 6 ? Print the result accordingly back to the screen.
Example
package main import ( "fmt" ) func main() { var i, j int matrixA := [3][3]int{ {0, 1, 2}, {4, 5, 6}, {8, 9, 10}, } matrixB := [3][3]int{ {10, 11, 12}, {13, 14, 15}, {16, 17, 18}, } fmt.Println("The first matrix is:") for i = 0; i < 3; i++ { for j = 0; j < 3; j++ { fmt.Print(matrixA[i][j], "\t") } fmt.Println() } fmt.Println() fmt.Println("The second matrix is:") for i = 0; i < 3; i++ { for j = 0; j < 3; j++ { fmt.Print(matrixB[i][j], "\t") } fmt.Println() } fmt.Println() if matrixA == matrixB { fmt.Println("The First Matrix and the Second Matrix are Equal") } else { fmt.Println("The First Matrix is Not Equal to the Second Matrix") } }
Output
The first matrix is: 0 1 2 4 5 6 8 9 10 The second matrix is: 10 11 12 13 14 15 16 17 18 The First Matrix is Not Equal to the Second Matrix
Check Whether Two Matrices are Equal or Not Using For Loop
Let us now look at another method using which we can check the equality of two matrices.
Algorithm
Step 1 ? Import the fmt package.
Step 2 ? Start the main() function.
Step 3 ? Initialize and assign values to two matrices.
Step 4 ? Print these matrices on the screen. Now use the for loops to check whether each element of one matrix is equal to the elements of other or not.
Step 5 ? Initialize a variable called isequal and assign it a value of one. Use for loops to iterate over the matrix.
Step 6 ? If any of the element of the two matrices comes out to be different then flip the value of isequal to zero and break the loop.
Step 7 ? Once the loop is completed use the if condition to equate the value of isequal variable whose value comes out to be zero only if the elements are not same.
Step 8 ? Now print the result on the screen.
Example
package main import ( "fmt" ) func main() { var i, j int matrixA := [3][3]int{ {0, 1, 2}, {4, 5, 6}, {8, 9, 10}, } matrixB := [3][3]int{ {0, 1, 2}, {4, 5, 6}, {8, 9, 10}, } fmt.Println("The first matrix is:") for i = 0; i < 3; i++ { for j = 0; j < 3; j++ { fmt.Print(matrixA[i][j], "\t") } fmt.Println() } fmt.Println() fmt.Println("The second matrix is:") for i = 0; i < 3; i++ { for j = 0; j < 3; j++ { fmt.Print(matrixB[i][j], "\t") } fmt.Println() } fmt.Println() isequal := 1 for i = 0; i < 3; i++ { for j = 0; j < 3; j++ { if matrixA[i][j] != matrixB[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") } }
Output
The first matrix is: 0 1 2 4 5 6 8 9 10 The second matrix is: 0 1 2 4 5 6 8 9 10 The First Matrix and the Second Matrix are Equal
Conclusion
We have successfully compiled and executed a golang code to check whether the two matrices are equal or not.