Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 5, Problem 5.1PE

(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. Here are sample runs:

Enter an integer, the input ends if it is 0: 1 2 -1 3 0  Chapter 5, Problem 5.1PE, (Count positive and negative numbers and compute the average of numbers) Write a program that reads , example  1

The number of positives is 3

The number of negatives is 1

The total is 5.0

The average is 1.25

Enter an integer, the input ends if it is 0: 0 Chapter 5, Problem 5.1PE, (Count positive and negative numbers and compute the average of numbers) Write a program that reads , example  2

No numbers are entered except 0

Expert Solution & Answer
Check Mark
Program Plan Intro

Count positive and negative numbers and compute the average of numbers

Program Plan:

  • Include the required import statement.
  • Define the class
    • Define the main() method using public static main.
      • Declare and initialize the required variables.
      • Declare the input scanner.
      • Read an input from the user.
      • Using while loop, check whether the integer is “0” or not
        • Check whether the integer is greater than “0”.
          • If so, increment the positive counter.
        • Check whether the integer is less than “0”.
          • If so, increment the negative counter.
        • Calculate the sum of integers.
        • Read the next input.
      • Display the sum and average of integers.
Program Description Answer

The below program is used to count number of positives and number of negatives which are presented as inputs and finally calculate its sum and average as follows:

Explanation of Solution

Program:

//import statement

import java.util.Scanner;

//class Excersise_1

public class Excersise_1 {

// main function

public static void main(String[] args) {

// declare and initialize the required variables

int count_Positive = 0, count_Negative = 0;

int counter = 0, sum = 0, integer;

// declare the input scanner

Scanner in = new Scanner(System.in);

// print the instruction

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

// read the integer value from user

integer = in.nextInt();

// using while loop, check the integer

while (integer != 0) {

// check if it is positive

if (integer > 0)

/* if so, increment the positive counter */

count_Positive++;

// check if it is negative

else if (integer < 0)

/* if so, increment the negative counter */

count_Negative++;

// calculate the total of integer

sum += integer;

// increment the counter

counter++;

// Read the next integer

integer = in.nextInt();

}

// check the counter is 0

if (counter == 0)

// if so, no inputs are read

System.out.println("No numbers are entered except 0");

else {

// print the number of positive integers

System.out.println("The number of positives is " + count_Positive);

// print the number of negative integers

System.out.println("The number of negatives is " + count_Negative);

// print the sum

System.out.println("The total is " + sum);

// print the overall average

System.out.println("The average is " + sum * 1.0 / counter);

}

}

}

Sample Output

Enter an integer, the input ends if it is 0: 1

2

-1

3

0

The number of positives is 3

The number of negatives is 1

The total is 5

The average is 1.25

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
08:49
Students have asked these similar questions
Use While loop to solve the problems Note: Python language for solution 4. Write a program that uses a while loop, the program should do the following: The program should repeatedly read numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. 5. Write a program to find the sum of even numbers until n, where n is an int entered by the user, example: n = 11, sum = 2+4+6+8+10, sum = 30.
(IN C LANGUAGE) Cumulative Addition: Computer selects a number between 7 and 23 at random. User will only add 2, 3 or 5 numbers to reach that number.For example: To reach 14: User will enter 5 5 2 2 (4 input).Also he can enter 2 2 2 2 2 2 2 (7 input) or 3 3 3 3 2 (5 input). https://www.bartleby.com/questions-and-answers/in-c-language-cumulative-addition-computer-selects-a-number-between-7-and-23-at-random.-user-will-on/0509c740-d993-44ed-a468-7e02da552600
Alert dont submit AI generated answer.

Chapter 5 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)

Ch. 5.7 - Suppose the input is 2 3 4 5 0. What is the output...Ch. 5.7 - What does the following statement do? for ( ; ; )...Ch. 5.7 - If a variable is declared in a for loop control,...Ch. 5.7 - Convert the following for loop statement to a...Ch. 5.7 - Count the number of iterations in the following...Ch. 5.8 - Can you convert a for loop to a while loop? List...Ch. 5.8 - Can you always convert a while loop into a for...Ch. 5.8 - Identify and fix the errors in the following code:...Ch. 5.8 - Prob. 5.8.4CPCh. 5.9 - How many times is the println statement executed?...Ch. 5.9 - Show the output of the following programs. (Hint:...Ch. 5.11 - Will the program work if n1 and n2 are replaced by...Ch. 5.11 - In Listing 5.11. why is it wrong if you change the...Ch. 5.11 - In Listing 5. 11, how many times the loop body is...Ch. 5.11 - Prob. 5.11.4CPCh. 5.11 - Prob. 5.11.5CPCh. 5.12 - What is the keyword break for? What is the keyword...Ch. 5.12 - The for loop on the left is converted into the...Ch. 5.12 - Rewrite the programs TestBreak and TestContinue in...Ch. 5.12 - After the break statement in (a) is executed in...Ch. 5.13 - What happens to the program if (low high) in line...Ch. 5.14 - Simply the code in lined 27-32 using a conditional...Ch. 5 - (Count positive and negative numbers and compute...Ch. 5 - (Repeat additions) Listing 5.4,...Ch. 5 - (Conversion from kilograms to pounds) Write a...Ch. 5 - (Conversion from miles to kilometers) Write a...Ch. 5 - (Conversion from kilograms to pounds and pounds to...Ch. 5 - Prob. 5.6PECh. 5 - (Financial application: compute future tuition)...Ch. 5 - (Find the highest score) Write a program that...Ch. 5 - (Find the two highest scores) Write a program that...Ch. 5 - (Find numbers divisible by 5 and 6) Write a...Ch. 5 - (Find numbers divisible by 5 or 6, but not both)...Ch. 5 - (Find the smallest n such that n2 12,000) Use a...Ch. 5 - (Find the largest n such that n3 12,000) Use a...Ch. 5 - (Compute the greatest common divisor) Another...Ch. 5 - (Display the ASCII character table) Write a...Ch. 5 - (Find the factors of an integer) Write a program...Ch. 5 - (Display pyramid) Write a program that prompts the...Ch. 5 - (Display four patterns using Loops) Use nested...Ch. 5 - (Display numbers in a pyramid pattern) Write a...Ch. 5 - (Display prime numbers between 2 and 1,000) Modify...Ch. 5 - Prob. 5.21PECh. 5 - For the formula to compute monthly payment, see...Ch. 5 - (Demonstrate cancellation errors) A cancellation...Ch. 5 - Prob. 5.24PECh. 5 - (Compute ) You can approximate by using the...Ch. 5 - (Compute e) You can approximate e using the...Ch. 5 - (Display leap years) Write a program that displays...Ch. 5 - (Display the first days of each month) Write a...Ch. 5 - (Display calendars) Write a program that prompts...Ch. 5 - (Financial application: compound value) Suppose...Ch. 5 - (Financial application: compute CD value) Suppose...Ch. 5 - (Game: lottery) Revise Listing 3.8, Lottery.java,...Ch. 5 - (Perfect number) A positive integer is called a...Ch. 5 - (Game: scissor; rock, paper) Programming Exercise...Ch. 5 - (Summation) Write a program to compute the...Ch. 5 - (Business application: checking ISBN) Use loops to...Ch. 5 - (Decimal to binary) Write a program that prompts...Ch. 5 - (Decimal to octal) Write a program that prompts...Ch. 5 - (Financial application: find the sales amount) You...Ch. 5 - (Simulation: heads or tails) Write a program that...Ch. 5 - (Occurrence of max numbers) Write a program that...Ch. 5 - (Financial application: find the sales amount)...Ch. 5 - (Math: combinations) Write a program that displays...Ch. 5 - (Computer architecture: bit-level operations) A...Ch. 5 - (Statistics: compute mean and standard deviation)...Ch. 5 - (Reverse a string) Write a program that prompts...Ch. 5 - (Business: check ISBN-13) ISBN -13 is a new...Ch. 5 - (Process string) Write a program that prompts the...Ch. 5 - (Count vowels and consonants) Assume that the...Ch. 5 - Prob. 5.50PECh. 5 - (Longest common prefix) Write a program that...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Literals in Java Programming; Author: Sudhakar Atchala;https://www.youtube.com/watch?v=PuEU4S4B7JQ;License: Standard YouTube License, CC-BY
Type of literals in Python | Python Tutorial -6; Author: Lovejot Bhardwaj;https://www.youtube.com/watch?v=bwer3E9hj8Q;License: Standard Youtube License