Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // An array storing different ages
- int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};
- float avg, sum = 0;
- // 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 / ages.length;
- // Print the average
- System.out.println("The average age is: " + avg);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement