Write a array program to calculate average and sum in java

Write a array program to calculate average and sum in java. Calculate the sum and average of an array in Java. Enter the array’s size, followed by all of the array’s elements. Now, using the for loop, we calculate the sum of the array’s elements and divide it by the number of items in the array to get the average. the Java source code for calculating the sum and average of an array

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, sum = 0;
float average;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements you want in array");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements");
for(int i = 0; i < n ; i++)
{
a[i] = s.nextInt();
sum = sum + a[i];
}
System.out.println("Sum"+sum);
average = (int)sum / n;
System.out.println("Average"+average);
}
}

 

Output:

Enter number of elements you want in array 5
Enter all the elements
34
56
78
23
45
Sum236
Average47.0