Write a Java program that ask the user of the number of elements in array and input
elements as much as the number of elements. Print the inputted elements and determine the
sum and average of the array. (You must input 10 elements and varying 3-digit numbers).
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Array size: ");
int[] array = new int[in.nextInt()];
double sum = 0;
for (int i = 0; i < array.length; i++) {
array[i] = in.nextInt();
sum += array[i];
}
for (int element : array) {
System.out.println(element);
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + sum / array.length);
}
}
Comments
Leave a comment