How to Calculate the Average using Arrays in Golang? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Given an array of n elements, your task is to find out the average of the array.Approach: Accept the size of the array.Accept the elements of the array.Store the sum of the elements using for loop.Calculate Average = (sum/size of array)Print the average. Example: Input: n = 4 array = 1, 2, 3, 4 Output : sum = 10 average = 2.5 Go // Golang program to calculate the average of numbers in array package main import "fmt" func main() { // declaring an array of values array := []int{1, 2, 3, 4} // size of the array n := 4 // declaring a variable to store the sum sum := 0 // traversing through the array using for loop for i := 0; i < n; i++ { // adding the values of array to the variable sum sum += (array[i]) } // declaring a variable avg to find the average avg := (float64(sum)) / (float64(n)) // typecast all values to float // to get the correct result fmt.Println("Sum = ", sum, "\nAverage = ", avg) } Output Sum = 10 Average = 2.5 Here, n is the size of the array and sum is to store the sum of all the values of the array. Using a for loop we get the sum of the elements of the array. After calculating the sum, we must convert the data types of the sum and size of the array to float, so that we don't lose any decimal values.To know more approaches you can go through the article Program for the average of an array (Iterative and Recursive) Comment More infoAdvertise with us Next Article How to Calculate the Average using Arrays in Golang? M ManishKhetan Follow Improve Article Tags : Go Language Golang-Arrays Golang-Program Similar Reads How to calculate the average of a set of numbers? Answer: The formula for calculating the average is: Average=Sum of all values/Number of valuesThe mean or average of the given data distribution is the calculated central value. It is used to determine the average of the given data. It gives a measure of the central tendency of the data. The central 6 min read How to Calculate Moving Averages in Python? In this discussion we are going to see how to Calculate Moving Averages in Python in this discussion we will write a proper explanation What is Moving Averages?Moving Averages, a statistical method in data analysis, smooths fluctuations in time-series data to reveal underlying trends. Calculating th 11 min read How To Calculate Average Percentage with Examples To calculate an average percentage, you first add up all the individual percentages you want to average. Then, divide that total by the number of percentages you added together. This gives you the average. For example, if you have three percentages like 70%, 80%, and 90%, you add them up (240) and t 2 min read How to iterate over an Array using for loop in Golang? An array is a data structure of the collection of items of the similar type stored in contiguous locations. For performing operations on arrays, the need arises to iterate through it. A for loop is used to iterate over data structures in programming languages. It can be used here in the following wa 2 min read How to use for and foreach loop in Golang? There is only one looping construct in Golang, and that is the for loop. The for loop in Golang has three components, which must be separated by semicolons (;), those are:Â The initialization statement: which is executed before the first iteration. e.g. i := 0The condition expression: which is execu 3 min read Like