Java: Find the numbers greater than the average of the numbers of a specified array
Numbers Greater Than Average
Write a Java program that finds numbers greater than the average of an array.
Visual Presentation:

Sample Solution:
Java Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
// Initializing an array of integers
Integer nums[] = new Integer[]{1, 4, 17, 7, 25, 3, 100};
double sum = 0; // Initializing the sum variable
// Displaying the original array
System.out.println("Original Array: ");
System.out.println(Arrays.toString(nums));
// Calculating the sum of elements in the array
for(int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
}
// Calculating the average of the elements in the array
double average = (double) sum / nums.length;
// Displaying the average of the array
System.out.println("The average of the said array is: " + average);
System.out.println("The numbers in the said array that are greater than the average are: ");
// Printing numbers greater than the average in the array
for(int i = 0; i < nums.length; i++) {
if(nums[i] > average) {
System.out.println(nums[i]);
}
}
}
}
Sample Output:
Original Array: [1, 4, 17, 7, 25, 3, 100] The average of the said array is: 22.428571428571427 The numbers in the said array that are greater than the average are: 25 100
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Java program to find all numbers in an array that are less than the average value.
- Write a Java program to count the number of elements that exceed the average of an array.
- Write a Java program to compute the absolute deviation of each array element from the average.
- Write a Java program to partition an array into two arrays: one with numbers above the average and one with numbers below it.
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to find the kth smallest and largest element in a given array. Elements in the array can be in any order.
Next: Write a Java program that will accept an interger and convert it into a binary representation. Now count the number of bits which is equal to zero of the said binary represntation.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics