Java program to calculate the average value of array elements

import java.util.Scanner;
public class Average {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int sum=0;
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
sum=sum+a[i];
}
double avg=(double)sum/a.length;
System.out.println(avg);
}

}
i/p: 6
1 2 3 4 5 6
o/p: 3.5

No comments:

Post a Comment