Chapter: Miscellaneous Last Updated: 02-06-2023 05:09:16 UTC

Program:

` ````
/* ............... START ............... */
public class AverageCalculator {
public static void main(String[] args) {
int[] numbers = { 10, 20, 30, 40, 50 };
double average = calculateAverage(numbers);
System.out.println("The average is: " + average);
}
public static double calculateAverage(int[] numbers) {
int sum = 0;
for (int number : numbers) {
sum += number;
}
return (double) sum / numbers.length;
}
}
/* ............... END ............... */
```

Output

The average is: 30.0

Notes:

- The program you provided is a Java program that calculates the average of an array of numbers. The program first declares an array of integers called numbers and initializes it with the values 10, 20, 30, 40, and 50. The program then calls the calculateAverage() method, which calculates the average of the numbers in the numbers array. The calculateAverage() method first declares an integer variable called sum and initializes it to 0. The method then uses a for loop to iterate through the numbers array and add each number to the sum variable. The method then divides the sum variable by the length of the numbers array and returns the result. The main() method then prints the result of the calculateAverage() method.

Tags

Java program to find the average of an array of numbers #How do you find the average of numbers in an array

1 2