How to calculate the average of array of double values in Java

1 Answer

0 votes
package javaapplication1;
 
public class JavaApplication1 {
 
    public static void main(String[] args) {
 
        double arr[] = {3.5, 12.1, 13, 18.2, 44.1, 3.14};

        double sum = 0;
        int len = arr.length;

        for (int i = 0; i < len; i++) 
             sum = sum + arr[i];

	    System.out.print(sum/len);
    }
}
   
/*
run:
  
15.673333333333334
   
*/

 



answered Mar 18, 2017 by avibootz

Related questions

1 answer 163 views
2 answers 209 views
1 answer 162 views
2 answers 167 views
1 answer 173 views
2 answers 157 views
1 answer 153 views
...