GO Program to Matrix Multiplication Program

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

How to implement array multiplication program

For performing multiplication operation we need two arrays of same size. First read the elements of both the arrays. Traverse both the arrays from the initial position. Multiply the respective elements of both the arrays and store the result in another array.

example

assume that the first array is arr1 and the second array is arr2

arr1[1] = 2

arr2[1] = 6

arr1[1] * arr2[1] = result[1]

2         *   6        =  12

How to implement array multiplication program using Go Program

Here we are showing how to implement an array multiplication program in the Go language. Here variable Arr1,Arr2,Arr3 holds the array elements. Another variable i used as the index for the loop. Use two different for loop to read the elements into two arrays Arr1 and Arr2.

syntax for for loop


for    initializer; condition;   incrementor {
}

Traverse both the arrays and find the array multiplication by iterating through the loop. Multiplication is done as (Arr1[i] * Arr2[i]) and stored it to new arrayArr3. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Declare the variable i

STEP 4: Read the arrays Arr1[], Arr2[] using for loop

STEP 5: Find the array multiplication in a for loop as  Arr1[i] * Arr2[i]

STEP 6: Store the result into an array Arr3 and continue the loop

STEP 7:Print the result array Arr3 using fmt.Println()

 

GO Source Code

                                          package main
import "fmt"

func main() {
    var i int
    var Arr1 [5]int
    var Arr2 [5]int
    var MArr [5]int

    fmt.Print("Enter the first array items = ")
    for i = 0; i < 5; i++ {
        fmt.Scan(&  Arr1[i])
    }
    fmt.Print("Enter the second array items = ")
    for i = 0; i < 5; i++ {
        fmt.Scan(& Arr2[i])
    }

    fmt.Print("The multiplication of two arrays = ")
    for i = 0; i < len(MArr); i++ {
        MArr[i] = Arr1[i] * Arr2[i]
        fmt.Print(MArr[i], "  ")
    }
    fmt.Println()
}
                                      

OUTPUT

Enter the first array items = 1 2 3 4 5
Enter the second array items = 5 6 7 8 9
The multiplication of two arrays = 5  12  21  32  45