1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /* Calculate Average value of Array elements using Java Example This Java Example shows how to calculate average value of array elements. */ public class CalculateArrayAverageExample { public static void main(String[] args) { //define an array int[] numbers = new int[]{10,20,15,25,16,60,100}; /* * Average value of array elements would be * sum of all elements/total number of elements */ //calculate sum of all array elements int sum = 0; for(int i=0; i < numbers.length ; i++) sum = sum + numbers[i]; //calculate average value double average = sum / numbers.length; System.out.println("Average value of array elements is : " + average); } } /* Output of Calculate Average value of Array elements using Java Example would be Average value of array elements is : 35.0 */ |

Thank you..THUMBS UP! Useful info!

instead of:

double average = sum / numbers.length;

better would be:

double average = (double)sum/numbers.length;

the average is in double precision then

I need help guys ðŸ™‚ How to compute average speed with a distance and time travel using a JFrame Form in Java . thaks.