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
Asked Mar 11, 2019
847 views

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.

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

Image Transcriptionclose

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

Expert Answer

Step 1

Java program to count posive and negative numbers and computing the total and average of numbers is as follows:

Step 2

Program:

//Import necessary header files
import java.util.*;

//Declare the class name
public class CountNumbers
{
//Main method
publicstaticvoid main(String[] args)
{
//Scanner object for input
Scanner sc = new Scanner (System.in);
 
//Declare the becessary variables
float tot = 0;
double avg = 0.0;
int neg = 0;
int pos = 0;
int numbers = 0;
int n;
 
//Print statement
System.out.println("Enter an integer, the input"+"ends if it is 0:");

//Get the input from the user
n = sc.nextInt();

//Read an unspecified number of itegers
while(n!=0)
{
//Check the condition
if (n > 0)

//True, increment it by 1
pos++;
 
//Check the condition
elseif(n<0)

//True, increment it by 1
neg++;
 
//Increment the numbers by 1
numbers++;

//Add the value to the total
tot += n;

//Read an unspecified number of itegers
n = sc.nextInt();
}

//Check the condition
if(numbers == 0)
{
//True, print statement
System.out.println("No n...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

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

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: example:"apple-&gt;orange-&gt;banana-&gt;pear"-&gt;NULLreturn "orange"Thanks!

A: ApproachIn the function longest(), we'll declare an integer named highestLength and assign 0 to it.N...

question_answer

Q: What type of data breach occurred involving the Facebook/Cambridge Analytica?

A: Data breaching involves intentional or unintentional leakage of confidential information to un autho...

question_answer

Q: Write a program in c++ to generate a random number between 1 - 100, and then display which quartile ...

A: Program plan:Include the required header files into the program.Define the main function.Declare the...

question_answer

Q: I am working on creating a logical entity relationship diagram for my database class. I am wondering...

A: Although the full problem description is not given so deducing the problem / requirements from the d...

question_answer

Q: Note: Numbers greater then 3,999 require symbols with lines over letters. We can't use these, so use...

A: Note: The given python program has been compiled and executed in "repl.it" online complier.Screensho...

question_answer

Q: I was asked to add the three types of comments to this class, each consisting of the same statement ...

A: Java documentation comment: This comment is used to create documentation API. The syntax for this co...

question_answer

Q: What language is accepted by the npda M = ({q0, q1, q2}, {a, b}, {a, b, z}} q0, z, {q2}). δ(q0,a,z)=...

A: The given npda is M = ({q0, q1, q2}, {a, b}, {a, b, z}} q0, z, {q2}) with transition functions asδ(q...

question_answer

Q: Describe, in English, the language defined by the following grammar in BNF (start symbol is ):

A: Solution:The given grammar is,&lt;S&gt; :: = &lt;A&gt; |&lt;B&gt;&lt;C&gt; :: = a | c&lt;A&gt; :: = ...

question_answer

Q: Design a program that displays a table of the Celsius temperatures 0 through 20 and their Fahrenheit...

A: Program plan:Use “for” loop to print a table of the Celsius temperatures 0 through 20 and their Fahr...