Allow a user to enter any number of double values up to 20. The user should ente
ID: 3769445 • Letter: A
Question
Allow a user to enter any number of double values up to 20. The user should enter 999999 to quit entering numbers. Display an error message if the user quits without entering any numbers; otherwise display each entered value and its distance from the average. Save the file as Distancefromaverage.jav
//chapter 8; java programming; joyce farrell, p 433, 8th edition
//2nd same question to be posted
//array
//example output
Enter number 1 to10.... 999999 to quit: 78.0
Enter number 1 to 10....999999 to quit: 55.0
Enter number 1 to 10.....999999 to quit: 999999
Entered 2 numbers and the average is 66.6
78.0 is -0.12857142857141923, distance away from the average
55.0 is -23.12857142857142, distance away from the average
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Distancefromaverage
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner input = new Scanner(System.in);
double[] arr = new double[20];
int i = 0;
for(i=0;i<20;i++)
{
System.out.println("Enter any value (999999 to quit) : ");
arr[i] = input.nextDouble();
if(arr[i]==999999)
break;
}
double avg = 0;
if(i!=0)
{
for(int j=0;j<i;j++)
avg = avg+arr[j];
avg = avg/i;
System.out.println("Entered "+i + " numbers and the average is : "+avg);
for(int j=0;j<i;j++)
System.out.println(arr[j]+" is "+(avg-arr[j])+" , distance away from the average");
}
else
System.out.println("Zero numbers inputted");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.