The program to compute average of an array of integers is a simple program to demonstrate the usage of arrays.The program is intended for beginners of Java programming language.
We are using JDK 8u111 with Netbeans IDE 8.2 installed on a Windows 7 64-bit PC to compile and run the program. You may alternatively install JDK and compile and run the program from windows command line.
Problem Definition
To compute the average of integers stored in an array, first, we receive the user input values for an array. The next step sums the array of integers and divides by the total number of integers. This will give the average values and you can now display the results.
Program Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javademo;
/**
*
* @author admin
*/
//Average of an array of values
class Average {
public static void main(String args[]){
double num[]={10.1,11.2,12.3,13.4,14.5};
double result = 0;
int l;
for(l=0;l<5;l++)
result=result+num[l];
System.out.println("Average is " + result/5);
}
}
Output
Average is 12.299999999999999
Related Articles:-