# Golang Program to add two arrays

March 1, 2022, Learn eTutorial
1725

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

## How to add two arrays

To add two arrays of numbers we have to traverse both the array simultaneously from the ent until the 0th index of either of the array. While traversing each element of both the array add the corresponding elements of both the array and store the result in another array.

## How to add two arrays in the GO Program

Here we are showing to add two arrays in the Go language. Here variable size holds the size of the array, and other variables Arr1, Arr2 holds the arrays to be added.  Variable sumArr holds the resultant array elements. First, read the array elements using `for loop`. Elements of both the arrays to be added is readed by using` for loop.`

syntax for` for loop`

``````
for    initializer; condition;   incrementor {
}
```
```

After that add the two arrays as sumArr[i] = Arr1[i] + Arr2[i].  For traversing the arrays  again` for loop` is used. The corresponding elements are added and the result is stored into sumArr. Finally, print the result array sumArr.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: Read the size of the array using `fmt.Scanfln()`

STEP 4: Declare the arrays variable Arr1, Arr2, sumArr

STEP 5: Read the first array using `for loop`

STEP 6: Read the second array using `for loop`

STEP 7: Add the two arrays by adding sumArr[i] = Arr1[i] + Arr2[i]

STEP 8: Print the result array sumArr using `fmt.Println()`

## Golang Source Code

```                                          ```package main
import "fmt"

func main() {
var size, i int
fmt.Print("Enter the array size = ")
fmt.Scanln(& size)

Arr1 := make([]int, size)
Arr2 := make([]int, size)
sumArr := make([]int, size)

fmt.Println("Enter the first array items = ")
for i = 0; i < size; i++ {
fmt.Scanln(& Arr1[i])
}

fmt.Println("Enter the second array items = ")
for i = 0; i < size; i++ {
fmt.Scanln(& Arr2[i])
}

fmt.Println("The sum of two arrays = ")
for i = 0; i < size; i++ {
sumArr[i] = Arr1[i] + Arr2[i]
fmt.Println(sumArr[i], "  ")
}
fmt.Println()
}```
```

## OUTPUT

```Enter the array size = 5
Enter the first array items = 1 2 3 4 5
Enter the second array items = 6 7 8 9 10
The sum of two arrays = 7  9  11  13  15 ```

VIEW ALL
VIEW ALL
VIEW ALL