For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:
The basic arithmetic operators are addition "+" , subtraction "-" , multiplication "*" , division "/"
example
addition - 2+2 = 4
subtraction- 4-1 = 3
multiplication - 2*3 = 6
division - 6/2 = 3
For performing these operations on array we need two arrays. First read elements into two different arrays and traverse the elements of both the arrays from the initial position. Take each of the element from both the arrays and perform the arithmetic operations.
example
let the first array arr1[] and the second array arr2[]
take the first elemant of both arr1[1] and arr2[1] and perform the operation like
arr1[1] + arr2[1] =
likewise continue the procedure with other corresponding elements to perform the remaining operators.
Here we are showing how to perform Arithmetic operations on an array in the Go language. Here variables arr1, arr2 hold the integer array elements. Use for loop
to find the results of each arithmetic operation.
syntax for for loop
https://learnetutorials.com/golang/loops-with-examples
Given below are the steps which are used in the Go program.
STEP 1: Import the package fmt
STEP 2: Start function main()
STEP 3: Declare two integer arrays arr1,arr2
STEP 4: Using the for loop
, perform addition, subtraction, division, multiplication, and remainder
STEP 5: Print the results using fmt.Println()
package main
import "fmt"
func main() {
arr1 := []int{10, 29, 70, 40, 127}
arr2 := []int{15, 25, 35, 45, 55}
fmt.Println("Add\tSub\tMul\tDiv\tMod")
for i := 0; i < 5; i++ {
fmt.Print("\n", arr1[i]+arr2[i], "\t")
fmt.Print(arr1[i]-arr2[i], "\t")
fmt.Print(arr1[i]*arr2[i], "\t")
fmt.Print(arr1[i]/arr2[i], "\t")
fmt.Print(arr1[i]%arr2[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