Write a java program called PositiveNegative that reads an unspecified number of
ID: 3758668 • Letter: W
Question
Write a java program called PositiveNegative that reads an unspecified number of integers, determines how many positive and negative values have been entered, and computes the sum and average of the input values (not counting zeros). The reading of input ends when the user enters 0 (zero). Display the number of positive and negative inputs, the sum and the average. The average should be computed as a double.
Design the program such that it asks the user if they want to continue with new inputs after each set of entries, ending the program only when they do not respond to the question with "yes".
Here is a sample run:
Explanation / Answer
Solution for above problem is below ..let me know ypour feed back as a comomnet
import java.util.Scanner;
public class Numner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int positive = 0, negative = 0, total = 0, count = 0;
double average;
System.out.println("Enter the number: ");
int number;
while ((number = input.nextInt()) != 0) {
total += number;
count++;
if (number > 0) {
positive++;
} else if (number < 0) {
negative++;
}
}
average = (double) total / count;
System.out.println("The number of positives is " + positive);
System.out.println("The number of negatives is " + negative);
System.out.println("The total is " + total);
System.out.printf("The average is: " + average);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.