The average of an array in java?

The average of an array in java?

Previous Home Next

 

In this program we will understand how the average of an Arraylist is find out.

This Java Example shows how to calculate average value of array elements.
 The average value of  an Arraylist =Sum of elements /Total number of elements

using sum=sum+numbers[i] 
first we did the sum of elements and then find the average using
double average= sum /numbers.length the value of average is then print .

 
public class AverageExample {
 
	public static void main(String[] args) {
  int[] numbers = new int[]{10,20,30, 40,50,}// define an array
  int sum = 0;
 
		for(int i=0; i < numbers.length ; i++) // sum the elemnt of an array
			sum = sum + numbers[i];
			double average = sum / numbers.length;
 
		System.out.println("Average value of array is : " + average);
	}
}

Average value of array is : 30
Previous Home Next