# Golang Program to Calculate the Average of Numbers in a Given List

Go ProgrammingServer Side ProgrammingProgramming

#### Cyber Security : Go from Zero to Hero

Most Popular

60 Lectures 5.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

Input array is: [2, 4, 1, 6, 5]

Sum = 2 + 4 + 1 + 6 + 5 => 18

Average = 18/5 => 3.6 ~ 3

To calculate the average of numbers in a given list, we can take following steps −

• Let's take an input list of numbers.
• Find the sum of numbers using sum() method.
• The sum method calculates the sum of numbers by iterating the given list.
• Print the average by dividing the sum with the length of the given list.

## Example

Live Demo

package main
import (
"fmt"
)
func sum(arr []int) int{
result := 0
for _, i :=range arr {
result += i
}
return result
}
func main() {
arr := []int{2, 4, 1, 6, 5}
fmt.Println("Given list is: ", arr)
res := sum(arr)
fmt.Println("Average of numbers is: ", res/len(arr))
}

## Output

Given list is: [2 4 1 6 5]
Average of numbers is: 3