bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Find the Average of Array Elements

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java How To Find the Average of Array Elements?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// An ___ storing different ages int ages[] = {20, 22, 18, 35, 48, 26, 87, 70}; float avg, sum = 0; // Get the length of the array int length = ages.length; // Loop through the elements of the array

How To Calculate the Average of Array Elements

Create a program that calculates the average of different ages:

Example

// An array storing different ages int ages[] = {20, 22, 18, 35, 48, 26, 87, 70}; float avg, sum = 0; // Get the length of the array int length = ages.length; // Loop through the elements of the array
for (int age : ages) {
  sum += age;
}
// Calculate the average by dividing the sum by the length avg = sum / length; // Print the average System.out.println("The average age is: " + avg);

Previous

Java How To Calculate the Sum of Elements

Next

Java How To Sort an Array