You are here: Home / Topics / Program to find average of elements of an array in Java

Program to find average of elements of an array in Java

Filed under: Java on 2023-08-19 18:37:36

// average of sum of array

public class SumAndAverage
{
   public static void main(String[] args)
   {
       int i,sum=0,avg=0;
       int a[]={10,20,30};
       for(i=0;i<a.length;i++)
       {
           sum+=a[i];
       }
       avg=sum/a.length;
       System.out.println("Average : "+avg);
   }
}


Output:

Average : 20

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.