Answer to Question #57964 in Java | JSP | JSF for kenya

Question #57964
Write a Java program that can add up and count a series (any number) of integers entered by the user. Use a loop and stop the loop when the user enters a value of zero. Then, display the count, total and average. Express the average accurate to three decimal places.

SAMPLE RUN
Enter a number or 0 to quit: 12
Enter a number or 0 to quit: 5
Enter a number or 0 to quit: 41
Enter a number or 0 to quit: 0
The total of those 3 numbers is 58.
The average is 19.333.
1
Expert's answer
2016-02-22T02:38:05-0500
import java.util.Scanner;


public class Average {

public static void main(String[] args) {

int number = 0;
int count = 0;
int summ = 0;
double average = 0;
Scanner scan = new Scanner(System.in);
do {
System.out.print("Enter a number or 0 to quit: ");
number = scan.nextInt();
if (number != 0) {
summ += number;
count++;
}
}
while (number != 0);
System.out.println("The total of those " + count + " numbers is " + summ + ".");
if (count !=0) {
average = 1.0 * summ / count;
System.out.printf("The average is %.3f.", average);
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS