Saturday, 1 August 2015

S2P2

Write a program to find the average of n numbers stored in an Array.


 class S2P2_avg  
 {  
      public static void main(String args[])  
      {  
           int a[] = {12,32,4,56,97,78,89,78,91,56,114};  
           float total = 0;  
           for(int i=0; i<a.length; i++)  
           {  
                total = total + a[i];  
           }  
           System.out.println("Average of given values is "+(total/a.length));                 
      }  
 }  

Output:

   
 E:\Java>javac S2P2_avg.java  
   
 E:\Java>java S2P2_avg  
 Average of given values is 64.27273  
   
 E:\Java>  
   

No comments:

Post a Comment