# 5-1) (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how manypositive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display theaverage as a floating-point numberSAMPLE RUN #4: java AvgwithLoopInteractive SessionHide InvisiblesHighlight: NoneShow Highlighted Only DEnter.an.integer, the.input ends.if.it.is.e:-4433.-12.15.89.45The-number-of. positives is . 6The number.of.negatives.is.3The total.is.335.0The average.is 37.22113.117.45.8

Question
2187 views

Use Java to answer the question.

5-1) (Count positive and negative numbers and compute the average of numbers)

Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. help_outlineImage Transcriptionclose5-1) (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number SAMPLE RUN #4: java AvgwithLoop Interactive SessionHide Invisibles Highlight: None Show Highlighted Only D Enter.an.integer, the.input ends.if.it.is.e:-4433.-12.15.89.45 The-number-of. positives is . 6 The number.of.negatives.is.3 The total.is.335.0 The average.is 37.22 113.117.45.8 fullscreen
check_circle

Step 1

Java code

import java.util.Scanner;

public class AvgWithLoop {

public static void main(String[] args){

int num;    //to store the input number

int positiveCount=0;    //to store the count of positive numbers

int negativeCount=0;    //to store the count of negative numbers

double total=0.0; //to store the total of all the numbers

double avg=0.0;   //to store the average of all the numbers

Scanner s=new Scanner(System.in);   //declare scanner object to take input from user

System.out.println("Enter an integer, the input ends if it is 0: ");

while(true){      //non terminating loop

num=s.nextInt();  //accept a number and store in num

if(num==0){ //if the input number is 0

break;      //terminate the loop

}

if(num>0){  //if it is a positive number

positiveCount+=1; //increase the positive count by 1

&n...

### Want to see the full answer?

See Solution

#### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

### Programing Language 