In this program, we will read elements for matrix1 and matrix2 from the user and then multiply both matrices and also print the matrix on the console screen.
The source code to multiply two matrices is given below. The given program is compiled and executed successfully.
// Golang program to multiply two matrices.
package main
import "fmt"
func main() {
var sum int = 0
var matrix1 [2][2]int
var matrix2 [2][2]int
var matrix3 [2][2]int
fmt.Printf("Enter matrix1 elements: \n")
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
fmt.Printf("Elements: matrix1[%d][%d]: ", i, j)
fmt.Scanf("%d", &matrix1[i][j])
}
}
fmt.Printf("Enter matrix2 elements: \n")
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
fmt.Printf("Elements: matrix2[%d][%d]: ", i, j)
fmt.Scanf("%d", &matrix2[i][j])
}
}
//Multiplication of matrix1 and matrix2.
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
sum = 0
for k := 0; k < 2; k++ {
sum = sum + matrix1[i][k]*matrix2[k][j]
}
matrix3[i][j] = sum
}
}
fmt.Printf("Matrix1: \n")
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
fmt.Printf("%d ", matrix1[i][j])
}
fmt.Printf("\n")
}
fmt.Printf("Matrix2: \n")
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
fmt.Printf("%d ", matrix2[i][j])
}
fmt.Printf("\n")
}
fmt.Printf("Multiplication of matrix1 and matrix2: \n")
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
fmt.Printf("%d ", matrix3[i][j])
}
fmt.Printf("\n")
}
}
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.
In the main() function, we created two 2X2 matrices using a two-dimensional array.
fmt.Printf("Enter matrix1 elements: \n")
for i:=0;i<2;i++{
for j:=0;j<2;j++{
fmt.Printf("Elements: matrix1[%d][%d]: ",i,j)
fmt.Scanf("%d",&matrix1[i][j])
}
}
fmt.Printf("Enter matrix2 elements: \n")
for i:=0;i<2;i++{
for j:=0;j<2;j++{
fmt.Printf("Elements: matrix2[%d][%d]: ",i,j)
fmt.Scanf("%d",&matrix2[i][j])
}
}
In the above code, we read elements for matrix1 and matrix2 from the user.
//Multiplication of matrix1 and matrix2.
for i:=0;i<2;i++{
for j:=0;j<2;j++{
sum=0
for k:=0;k<2;k++{
sum=sum+matrix1[i][k]*matrix2[k][j]
}
matrix3[i][j]=sum
}
}
In the above code, we performed the multiplication of two matrices and printed the result on the console screen.
Program/Source Code:
The source code to multiply two matrices is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.
In the main() function, we created two 2X2 matrices using a two-dimensional array.
In the above code, we read elements for matrix1 and matrix2 from the user.
In the above code, we performed the multiplication of two matrices and printed the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer