In this Go array multiplication program, we declared three arrays of fixed size. Within the for loop (for i = 0; i < len(MultiplicationArr); i++), we performed multiplication of two arrays and assigned it to Multiplication array.
package main import "fmt" func main() { var i int var multiArr1 [5]int var multiArr2 [5]int var MultiplicationArr [5]int fmt.Print("Enter the First Array Items for Multiplication = ") for i = 0; i < 5; i++ { fmt.Scan(&multiArr1[i]) } fmt.Print("Enter the Second Array Items for Multiplication = ") for i = 0; i < 5; i++ { fmt.Scan(&multiArr2[i]) } fmt.Print("The Multiplication of Two Arrays = ") for i = 0; i < len(MultiplicationArr); i++ { MultiplicationArr[i] = multiArr1[i] * multiArr2[i] fmt.Print(MultiplicationArr[i], " ") } fmt.Println() }
Enter the First Array Items for Multiplication = 1 2 3 4 5
Enter the Second Array Items for Multiplication = 9 11 22 3 7
The Multiplication of Two Arrays = 9 22 66 12 35
Go Program to Multiply Two Arrays Example
In this Golang example, we used the for loop range (for j := range MultiplicationArr) and performed the array multiplication.
package main import "fmt" func main() { var size, i int fmt.Print("Enter the Multiplication Array Size = ") fmt.Scan(&size) multiArr1 := make([]int, size) multiArr2 := make([]int, size) MultiplicationArr := make([]int, size) fmt.Print("Enter the First Array Items for Multiplication = ") for i = 0; i < size; i++ { fmt.Scan(&multiArr1[i]) } fmt.Print("Enter the Second Array Items for Multiplication = ") for i = 0; i < size; i++ { fmt.Scan(&multiArr2[i]) } fmt.Print("The Multiplication of Two Arrays = ") for j := range MultiplicationArr { MultiplicationArr[j] = multiArr1[j] * multiArr2[j] fmt.Print(MultiplicationArr[j], " ") } fmt.Println() }
Enter the Multiplication Array Size = 4
Enter the First Array Items for Multiplication = 10 20 30 40
Enter the Second Array Items for Multiplication = 7 8 9 10
The Multiplication of Two Arrays = 70 160 270 400
In this Golang program for multiplication of two arrays, we used for loop range to allocate the array items and multiply them.
package main import "fmt" func main() { var size, i int fmt.Print("Enter the Multiplication Array Size = ") fmt.Scan(&size) multiArr1 := make([]int, size) multiArr2 := make([]int, size) MultiplicationArr := make([]int, size) fmt.Print("Enter the First Array Items for Multiplication = ") for i := range multiArr1 { fmt.Scan(&multiArr1[i]) } fmt.Print("Enter the Second Array Items for Multiplication = ") for j := range multiArr2 { fmt.Scan(&multiArr2[j]) } fmt.Print("The Multiplication of Two Arrays = ") for k := range MultiplicationArr { MultiplicationArr[k] = multiArr1[k] * multiArr2[k] fmt.Print(MultiplicationArr[k], " ") } fmt.Println() }