Write a program that inputs 20 integers and stores them inside an array. Then, calculate the average of the numbers. Finally, use a for loop to find the sum of all the integers in the array that are greater than the average, and print this sum out.
To see this working, head to your live site.
Search
kevinzhao183
Aug 03, 2020
Intro to Java 8/1 Homework
Intro to Java 8/1 Homework
2 comments
0
import java.util.*;
public class Array {
public static void main(String[] args) {
int[] arr = new int[20];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 20; i++) {
int a = sc.nextInt();
arr[i] = a;
}
sc.close();
int sum = 0;
for (int i = 0; i < 20; i++) {
sum = arr[i] + sum;
}
int sum1 = 0;
int avg = sum / 20;
for (int i = 0; i < 20; i++) {
if (arr[i] > avg) {
sum1 = arr[i] + sum1;
}
}
System.out.println(sum1);
}
}
class Main { public static void main(String[] args) { int[] numbers = new int[20]; Scanner sc = new Scanner(System.in); for (int i = 0; i < 20; i++) { int a = sc.nextInt(); numbers[i] = a; } sc.close(); int add = 0; for (int i = 0; i < 20; i++) { add = numbers[i] + add; } int add1 = 0; int avg = add / 20; for (int i = 0; i < 20; i++) { if (numbers[i] > avg) { add1 = numbers[i] + add1; } } System.out.println(add1); } }