fork(1) download
  1. public class Main {
  2. public static void main(String[] args) {
  3. // An array storing different ages
  4. int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};
  5.  
  6. float avg, sum = 0;
  7.  
  8. // Get the length of the array
  9. int length = ages.length;
  10.  
  11. // Loop through the elements of the array
  12. for (int age : ages) {
  13. sum += age;
  14. }
  15.  
  16. // Calculate the average by dividing the sum by the length
  17. avg = sum / length;
  18.  
  19. // Print the average
  20. System.out.println("The average age is: " + avg);
  21. }
  22. }
  23.  
Success #stdin #stdout 0.12s 57852KB
stdin
Standard input is empty
stdout
The average age is: 40.75