In this Go Program to Perform Arithmetic Operations on an Array, we declared two integer arrays of different numbers. Within the for loop, we performed addition, subtraction, division, multiplication, and remainder.
package main import "fmt" func main() { garr1 := []int{10, 29, 70, 40, 127} garr2 := []int{15, 25, 35, 45, 55} fmt.Println("Add\tSub\tMul\tDiv\tMod") for i := 0; i < 5; i++ { fmt.Print("\n", garr1[i]+garr2[i], "\t") fmt.Print(garr1[i]-garr2[i], "\t") fmt.Print(garr1[i]*garr2[i], "\t") fmt.Print(garr1[i]/garr2[i], "\t") fmt.Print(garr1[i]%garr2[i], "\t") } fmt.Println() }
Add Sub Mul Div Mod
25 -5 150 0 10
54 4 725 1 4
105 35 2450 2 0
85 -5 1800 0 40
182 72 6985 2 17
Go Program to Perform Arithmetic Operations on an Array.
This Golang program allows users to enter the array size, first array elements, and second array items. Next, we use the arithmetic operators to perform the arithmetic operations.
package main import "fmt" func main() { var size, i int fmt.Print("Enter the Array Size = ") fmt.Scan(&size) garr1 := make([]int, size) garr2 := make([]int, size) fmt.Print("Enter the First Array Items = ") for i = 0; i < size; i++ { fmt.Scan(&garr1[i]) } fmt.Print("Enter the Second Array Items = ") for i = 0; i < size; i++ { fmt.Scan(&garr2[i]) } fmt.Println("Add\tSub\tMul\tDiv\tMod") for i = 0; i < size; i++ { fmt.Print("\n", garr1[i]+garr2[i], "\t") fmt.Print(garr1[i]-garr2[i], "\t") fmt.Print(garr1[i]*garr2[i], "\t") fmt.Print(garr1[i]/garr2[i], "\t") fmt.Print(garr1[i]%garr2[i], "\t") } fmt.Println() }