Java: Calculate the average value of array elements
Java Array: Exercise-4 with Solution
Write a Java program to calculate the average value of array elements.
Pictorial Presentation:
Sample Solution:
Java Code:
// Define a class named Exercise4.
public class Exercise4 {
// The main method where the program execution starts.
public static void main(String[] args) {
// Declare an integer array 'numbers' and initialize it with values.
int[] numbers = new int[]{20, 30, 25, 35, -16, 60, -100};
// Initialize a variable 'sum' to store the sum of array elements.
int sum = 0;
// Use a for loop to iterate over the elements of the 'numbers' array.
for (int i = 0; i < numbers.length; i++) {
// Add the current element to the sum.
sum = sum + numbers[i];
}
// Calculate the average value by dividing the sum by the number of elements.
double average = sum / numbers.length;
// Print the calculated average value to the console.
System.out.println("Average value of the array elements is : " + average);
}
}
Sample Output:
Average value of the array elements is : 7.0
Flowchart:
Java Code Editor:
Previous: Write a Java program to print the following grid.
Next: Write a Java program to test if an array contains a specific value.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/array/java-array-exercise-4.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics