Home Java Beginners Average of Array



Average of Array
Posted on: June 5, 2007 at 12:00 AM
In this section, we will learn how to get an average of array.

Average of Array

     

In this section, we will learn how to get an average of array. For this, first of all we have to define a class name "ArrayAverage" that has double type array to contain some values. Now we take a double type data that calculates the average of array (result/nums.length). And finally it will display the result on the command prompt with message by using the System.out.println().

 

Here is the code of this program

 

 

public class ArrayAverage{
  public static void main(String[] args) {
  double nums[]={1.0,2.3,3.4,4.5,40.5};
  double result=0.0;
  int i=0;
  for(i=0; i < nums.length; i++){
  result=result + nums[i];
  }
  System.out.println("Average is =" + result/nums.length);
  }
}

Download this Example

Related Tags for Average of Array:
ccomarrayclassdataprinttypesystemdisplaycommanddoublevaluenameusingintthismessageailengthpromptdefineforfinalwithaveragevaluestofinallyeilitcommasplulresultpemanfirstinnocalascalcmntoutplaysagecacalculatelnispclesemallagemeprodosysssussaspsoessatlatekisirhallrayandarsavassrithavstaluhatdispfinintlplprndonomo


More Tutorials from this section

Ask Questions?    Discuss: Average of Array   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.