Write a Java program that allows a user to enter 10 integer values into an array
ID: 3933854 • Letter: W
Question
Write a Java program that allows a user to enter 10 integer values into an array within a loop. The program should then display statistics about the data entered in the array, including: The largest value in the array The smallest value in the array The sum of all values in the array The average of the values in the array Use one loop to enter data into the array and for any needed calculations. All the code for this in-class assignment can be written within the public static void main(String[] args) method. Here is a sample transaction with the user: Please enter value #1: 2 Please enter value #2: 4 Please enter value #3: 6 Please enter value #4: 8 Please enter value #5: 10 Please enter value #6: 1 Please enter value #7: 3 Please enter value #8: 5 Please enter value #9: 7 Please enter value #10: 9 The largest value in the array is: 10 The smallest value in the array is: 1 The sum of values in the array is: 55.0 The average value in the array is: 5.5Explanation / Answer
Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
int n=10, max, min;
float sum = 0, avg;
Scanner s = new Scanner(System.in);
int a[] = new int[n];
System.out.println("Enter elements of array:");
for(int i = 0; i < n; i++){
System.out.print("Please enter the value #"+(i+1)+":");
a[i] = s.nextInt();
}
min=a[0];
max=a[n-1];
for(int i = 0; i < n; i++){
if(max < a[i]){
max = a[i];
}
if(min > a[i]){
min = a[i];
}
sum = sum + a[i];
}
avg = sum / (float)n;
System.out.println();
System.out.println("The largest value in the array is:"+max);
System.out.println("The smallest value in the array is:"+min);
System.out.println("The sum of values in the array is:"+sum);
System.out.println("The average value in the array is:"+avg);
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.