Golang program to multiply two matrices

Here, we are going to learn how to multiply two matrices in Golang (Go Language)?
Submitted by Nidhi, on March 09, 2021 [Last updated : March 03, 2023]

How to multiply two matrices in Golang?

Problem Solution:

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.

Program/Source Code:

The source code to multiply two matrices is given below. The given program is compiled and executed successfully.

Golang code to multiply two matrices

// 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")
	}
}

Output:

Enter matrix1 elements:
Elements: matrix1[0][0]: 1
Elements: matrix1[0][1]: 2
Elements: matrix1[1][0]: 3
Elements: matrix1[1][1]: 4
Enter matrix2 elements:   
Elements: matrix2[0][0]: 5
Elements: matrix2[0][1]: 6
Elements: matrix2[1][0]: 7
Elements: matrix2[1][1]: 8
Matrix1:
1 2
3 4
Matrix2:
5 6
7 8
Multiplication of matrix1 and matrix2:
19 22
43 50

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.

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.

Golang Array Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.