get the average of an array in java

63

get the average of an array in java -

double[] array = {19, 12, 16, 200, 13}; //Array
double total = 0; //Initialising total variable
double average = 0; //Initialising average variable

for(int i=0; i<array.length; i++){ // Looping through to retrieve total value
  total += array[i];
}

average = total / array.length; // Dividing the total by the length of the array OR number of numbers to get the average.

System.out.println("The average is: " + average);

Comments

Submit
0 Comments