GO Program to perform Arithmetic operations on an array

How to perform Arithmetic operations on an array

Here we are explaining how to perform Arithmetic operations on an array in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package and it helps to write standard output. In order to use these functions, we need to import the “fmt” package.

 

How to perform Arithmetic operations on an array using GO Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to perform Arithmetic operations on an array in the Go language. Here variables ar1, ar2 hold the integer array elements. Use for loop to find the results of each arithmetic operation. 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 two integer arrays ar1,ar2

STEP 4: Using the for loop, perform addition, subtraction, division, multiplication, and remainder

STEP 5: Print the results using fmt.Println()

 

GO Source Code

                                          package main

import "fmt"

func main() {

    ar1 := []int{10, 29, 70, 40, 127}
    ar2 := []int{15, 25, 35, 45, 55}

    fmt.Println("Add\tSub\tMul\tDiv\tMod")
    for i := 0; i < 5; i++ {
        fmt.Print("\n", ar1[i]+ar2[i], "\t")
        fmt.Print(ar1[i]-ar2[i], "\t")
        fmt.Print(ar1[i]*ar2[i], "\t")
        fmt.Print(ar1[i]/ar2[i], "\t")
        fmt.Print(ar1[i]%ar2[i], "\t")
    }
    fmt.Println()
}
                                      

OUTPUT

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