To Find the sum of the array elements we have to add all the elements in the array. Here we have to read the values and add that to our array. Set a variable s=0 initially to hold the sum. Then traverse the array from the first position. Add the first element with 's' and store the current value in s. Continue the procedure with all elements. Finally, we get the sum from the variable s.
In this Go program, to add all the elements in an array, we have to declare an array variable Arr. declare other variables Sum, i used as result and index variables. Use for loop
to find the sum of elements from i=0 to i less than the size of the array. After that add the array elements as Sum = Sum + Arr[i] in a loop. Finally, print the result.
STEP 1: Import the package fmt
STEP 2: Start function main()
STEP 3: Declare the variable Sum, i
STEP 4: Define the array Arr[] with 5 elements and Sum=0
STEP 5: Start a for loop
for adding array elements from i=0 to i
STEP 6: Find the sum as Sum = Sum + Arr[i]
STEP 7:Print the result Sum using fmt.Println()
To find the sum of array elements we have to know the below GO programming concepts, we recommend you to learn those for a better understanding
package main
import "fmt"
func main() {
Arr := []int{5, 10, 15, 20, 25}
Sum := 0
for i := 0; i < len(Arr); i++ {
Sum = Sum + Arr[i]
}
fmt.Println("The sum of array items = ", Sum)
}
The sum of array items = 75