JavaHow To Find the Average of Array Elements
How To Calculate the Average of Array Elements
Create a program that calculates the average of different ages:
Example
// An array storing different agesint ages[] = {20, 22, 18, 35, 48, 26, 87, 70};float avg, sum = 0;// Get the length of the arrayint length = ages.length;// Loop through the elements of the arrayfor (int age : ages) { sum += age;}// Calculate the average by dividing the sum by the lengthavg = sum / length;// Print the averageSystem.out.println("The average age is: " + avg);