Home java How to find the average arithmetic all elements of the array?

# How to find the average arithmetic all elements of the array?

Author

Date

Category

For example:

``````int [] numbers = {5, 8, 12, -18, -54, 84, -35, 17 , 37};
``````

How to find the average?
What algorithm, or maybe there are special functions for this?

Algorithm itself, which works for all Java versions:

``````// average arithmetic - the sum of all numbers divided by their number
int [] numbers = {5, 8, 12, -18, -54, 84, -35, 17, 37};
Double average = 0;
if (numbers.length & gt; 0)
{
Double Sum = 0;
For (int j = 0; j & lt; numbers.length; j ++) {
SUM + = Numbers [j];
}
average = sum / numbers.length;
}
``````

Well for example :

``````intstream.of (numbers) .AVerage ();
``````

This is Java 8, Stream API. Check: http://ideone.com/hsng8i

``````optionalDouble average = arrays.stream (Numbers) .AVerage ();
``````

``````class avarage {
Public Static Void Main (String Args []) {
INT NUM [] = {5, 8, 12, -18, -54, 84, -35, 17, 37};
Double Sum = 0;
For (int x: num) {
SUM + = X;
}
System.out.Print ("The average arithmetic numbers is:" + sum / num.length);
}
}
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.