- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Calculate the Average of Numbers in a Given List
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
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
- Related Articles
- Golang Program to Calculate Average Using Arrays
- Java program to calculate the average of numbers in Java
- C++ Program to Calculate Average of Numbers Using Arrays
- Golang program to calculate the sum of all even numbers
- Write a Golang program to calculate the sum of elements in a given array
- Golang Program to reverse a given linked list.
- Java program to calculate mean of given numbers
- Java program to find the average of given numbers using arrays
- Golang Program to Print the Sum of all the Positive Numbers and Negative Numbers in a List
- Golang program to calculate the sum of all odd numbers up to N
- Golang Program to add the first node in a given linked list.
- Calculate average of numbers in a column MySQL query?
- Golang program to calculate the area of a cube
- Golang program to calculate the volume of a cube
- How to Calculate the Sum of Natural Numbers in Golang?

Advertisements