Given an array of n elements, your task is to find out the average of the array.
- 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.
Input: n = 4 array = 1, 2, 3, 4 Output : sum = 10 average = 2.5
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 find 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)
- Average of max K numbers in a stream
- Golang | Polymorphism Using Interfaces
- How to Create Custom Errors using New Function in Golang?
- Using WaitGroup in Golang
- How to iterate over an Array using for loop in Golang?
- How to create a Struct Instance Using a Struct Literal in Golang?
- How to instantiate Struct using new keyword in Golang?
- Golang Program that Removes Duplicates Using Nested Loops
- How to Fix Race Condition using Atomic Functions in Golang?
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- How to Split Text Using Regex in Golang?
- Matching using regexp in GoLang
- How to calculate the difference between two dates in PHP?
- How to calculate percent minus px in SASS ?
- How to calculate total time of an array in PHP ?
- How to calculate P Value?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.