Engineering

Computer ScienceQ&A Library5-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.8Question

Asked Mar 13, 2019

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.

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...

Tagged in

Find answers to questions asked by student like you

Show more Q&A

Q: Design a Turing machine that computes the function f (x) = x − 2 if x > 2 = 0 i...

A: Click to see the answer

Q: Create a new file in c++ and save it as lab11_XYZ.cpp (replace XYZ with your initials). Create ANOTH...

A: Program Instructions:In the header file, create a temporary variable temp of the data type of each v...

Q: I am trying to read in a text file called "lines.txt" that holds an entire dictionary of words with ...

A: First you need to use the ifstream class object to read from file.

Q: How do I define a recursive function with C++? I am supposed to compute the Fibonacci numbers with a...

A: The algorithm for the recursive Fibonacci series is as below:Start with the first element which is 0...

Q: What values will print? Use Eclipse to verify it and show the steps. double x = Math.sqrt(4);System....

A: Creating the program exemplifying the Java math operators in a class called MathOperators as shown i...

Q: You need to use Eclipse to create this program.

A: Instructions for the first program:Create an array of size 5 and initialize a counter variable named...

Q: Visual Basic. Write the definition of a function named quadratic that receives three double paramete...

A: Formula for quadratic equation:

Q: I am struggling on storing numbers from the getChoice () function into an array and pulling it back ...

A: Click to see the answer

Q: Find a regular expression for the set {anbm : n ≥ 3, m is odd}

A: Regular expression:Given: {an bm: n >=3, m is odd}