Advertisement
Vladkoheca

Sredna vuzrast.java

Oct 22nd, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.35 KB | Source Code | 0 0
  1. // An array storing different ages
  2. int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};
  3.  
  4. float avg, sum = 0;
  5.  
  6. // Loop through the elements of the array
  7. for (int age : ages) {
  8.   sum += age;
  9. }
  10.  
  11. // Calculate the average by dividing the sum by the length
  12. avg = sum / ages.length;
  13.  
  14. // Print the average
  15. System.out.println("The average age is: " + avg);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement