Question

Write a small JAVA program that continually reads in user values and calculates the average of all values. The program s...

Write a small JAVA program that continually reads in user values and calculates the average of all values. The program should first ask the user for the number of values to be entered. Upon storing this value, the program should proceed to read in that amount of values.

What will be the best strategy here? a suggestion would be only calculating the average after you know you have read in all values. You can expect to use some type of loop to continually read in the values.

Hint:     Since we will know specifically how many values we need to read, a for-loop may be the best fit.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class Main
{
//strategy is
//first read the number of values to be read
//then read them by one by one and add them to a variable called sum
//now after completely reading and adding them to sum
//divide the sum by number of values
//and display the result
//only one loop is enough
public static void main(String[] args) {

System.out.println("Enter nunber of values to read :");
int n;
Scanner sc = new Scanner(System.in);//to readinput
n=sc.nextInt();
double sum=0;
int k;
System.out.println("Enter "+n+" values:");
for(int i=0;i<n;i++)
{
k=sc.nextInt();
sum+=k;
}
double averge = sum/n;
System.out.println("Average :"+averge);

}
}

Add a comment
Know the answer?
Add Answer to:
Write a small JAVA program that continually reads in user values and calculates the average of all values. The program s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Write a program that calculates the average of a stream of non-negative numbers. The user can...

    Write a program that calculates the average of a stream of non-negative numbers. The user can enter as many non-negative numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, zero counts as a number that goes into the average. Of course, the negative number should not be part of the average (and, for this program, the average of 0 numbers is 0). You must use a method to read...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Write a Java program that reads in a word from the user and outputs each character...

    Write a Java program that reads in a word from the user and outputs each character of the word on a separate line. For example, if the user enters the input "Joe", your program should output: J o e You need to use a for-loop.

  • In Python 3, Write a program that reads a set of floating-point values. Ask the user...

    In Python 3, Write a program that reads a set of floating-point values. Ask the user to enter the values, then print: The number of values entered. The smallest of the values The largest of the values. The average of the values. The range, that is the difference between the smallest and largest values. If no values were entered, the program should display “No values were entered” The program execution should look like (note: the bold values are sample user...

  • Task 1 : Write a Java program that prompts for and reads 10 integers from the...

    Task 1 : Write a Java program that prompts for and reads 10 integers from the user into an integer array of size 10, it then: - calculates the sum and average of positive numbers of the array and displays them. [Note: consider 0 as positive] Note: The program must display the message: "There are no positive values" if the array does not contain any positive value.

  • Write a Java program that reads in 5 words from the user and stores these words...

    Write a Java program that reads in 5 words from the user and stores these words in a String array. Then your program needs to print 2 lines of output with the following information: All words are in reverse order (based both on content and order). Every word at an even index of the array (i.e., indices 0, 2, and 4). For example, if user inputs "ABC", "DEF", "TY", "JK", and "WE". Then your program should output the following two...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • Write a program that prompts the user for student grades, calculates and displays the average grade...

    Write a program that prompts the user for student grades, calculates and displays the average grade in the class. The user should enter a character to stop providing values.

  • Write a complete java program to calculates the total of all the values in even positions...

    Write a complete java program to calculates the total of all the values in even positions in an array. Note If the input value for the array {1,2,3,4,5,6,7} the output should be: Problems Javadoc Declaration Console <terminated> ch7 [Java Application]/Library/Java/JavaVirtual Machines/jdk-1 The Total of the vaules in the even postion is : 16

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT