# Go – Program to multiply two matrix in go

Here, we will write a program to multiply two matrix in go. Given below, you can find algorithm and program.

Here, we are going to learn algorithm and program to multiply two matrix:

• Take number of rows and columns as input
• Apply nested for loop to take matrix elements as input
• Apply multiplication on matrix elements
• Print the resultant matrix

Number of column of first matrix and number of rows of second matrix should be same in matrix multiplication.

Size of the resultant matrix would be, matrix_1 rows * matrix_2 column

Example:

```Input:
Matrix_1: mat_1[2][3]

Matrix_2: mat_2[3][3]

Here, mat_1 column is equal to mat_2 row.

Size of resultant matrix:
result[2][3]

row = 2, column = 3

Matrix_1:
[
2, 3, 1
2, -7, 4
]

row = 3, column = 3

Matrix_2:
[
3, 4, 5
1, 1, 4
2, 1, 4
]

Output:
[
11, 12, 26
7, 5, -2
]```

Code:

``````package main

import (

"fmt"

)

func main() {

var mat_row_1, mat_col_1 int

var mat_row_2, mat_col_2 int

var mat_1, mat_2, result [10][10]int

fmt.Print("Enter no of rows of mat_1: ")

fmt.Scanln(&mat_row_1)

fmt.Print("Enter no of column of mat_1: ")

fmt.Scanln(&mat_col_1)

fmt.Print("Enter no of rows of mat_2: ")

fmt.Scanln(&mat_row_2)

fmt.Print("Enter no of column of mat_2: ")

fmt.Scanln(&mat_col_2)

fmt.Println("\nEnter matrix_1 elements: ")

for i := 0; i < mat_row_1; i++ {

for j := 0; j < mat_col_1; j++ {

fmt.Scanf("%d ", &mat_1[i][j])

}

}

fmt.Println("\nEnter matrix_2 elements: ")

for i := 0; i < mat_row_2; i++ {

for j := 0; j < mat_col_2; j++ {

fmt.Scanf("%d ", &mat_2[i][j])

}

}

// Multiplication of two matrix

for i := 0; i < mat_row_1; i++ {

for j := 0; j < mat_col_2; j++ {

result[i][j] = 0

for k := 0; k < mat_col_2; k++ {

result[i][j] += mat_1[i][k] * mat_2[k][j]

}

}

}

fmt.Println("\nAfter Multiplication Matrix is: \n")

for i := 0; i < mat_row_1; i++ {

for j := 0; j < mat_col_2; j++ {

fmt.Printf("%d ", result[i][j])

}
fmt.Println("\n")

}
``````

Output:

```Enter no of rows of mat_1: 2
Enter no of column of mat_1: 3

Enter no of rows of mat_2: 3
Enter no of column of mat_2: 3

Enter matrix_1 elements:
2
3
1
2
-7
4

Enter matrix_2 elements:
3
4
5
1
1
4
2
1
4

After Multiplication Matrix is:

11 12 26
7 5 -2```

`https://www.techieindoor.com/go-lang/`

References:

```https://golang.org/doc/
https://golang.org/pkg/```

Posted in golang, golang program