Question

(While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers....

(While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers. Your program ends with the input 0. Determine and display how many positive and negative values have been read, the maximum and minimum values, and the total and average of the input values (not counting the final 0).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CountPositiveNegativeNos.java

import java.util.ArrayList;
import java.util.Scanner;

public class CountPositiveNegativeNos {

   public static void main(String[] args) {
       // Declaring variables
       int num, count_negative = 0, count_positive = 0, total_nos, flag = 0, count = 0;
       double average = 0.0, sum = 0.0;

       // Creating an ArraList Object
       ArrayList ar = new ArrayList();

       // Scanner class object is used to read the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       /*
       * This while loop continues to execute until the user enters zero as
       * input
       */
       while (true) {

           // Getting the input entered by the user
           System.out.print("Enter a integer,the input ends if it is zero :");
           num = sc.nextInt();

           /*
           * checking If the input number is less than zero if yes,count the
           * negative numbers
           */
           if (num < 0) {
               // Counting the negative numbers
               count_negative++;
               count++;
               // Calculating the sum
               sum += num;
               continue;
           }
           /*
           * checking If the input number is greater than zero if yes,count
           * the positive numbers
           */
           else if (num > 0) {
               // Count positive numbers
               count_positive++;
               count++;
               sum += num;
               continue;
           }

           /*
           * checking If the input number is equal to zero if yes,Display the
           * message
           */
           else if (num == 0) {
               count++;
               break;
           }

       }

       if (count == 1) {
           System.out.println("No numbers are entered except 0");

       } else if (count > 1) {
           // total positive and negative numbers
           total_nos = count_negative + count_positive;

           // Calculating the average
           average = sum / total_nos;

           // Displaying the count of positive numbers
           System.out.println("The Number of Positives is " + count_positive);

           // Displaying the count of negative numbers
           System.out.println("The Number of Negatives is " + count_negative);

           // Displaying the sum of positive numbers and negative numbers
           System.out.println("The Total is " + sum);

           // Displaying the average of all the numbers
           System.out.println("The Average is " + average);

       }
   }

}

______________________________________

output1:

Enter a integer,the input ends if it is zero :1
Enter a integer,the input ends if it is zero :2
Enter a integer,the input ends if it is zero :-1
Enter a integer,the input ends if it is zero :3
Enter a integer,the input ends if it is zero :0
No numbers are entered except 0
The Number of Positives is 3
The Number of Negatives is 1
The Total is 5.0
The Average is 1.25

____Thank You

Add a comment
Know the answer?
Add Answer to:
(While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home 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 to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • Java programming 1. Write a program that reads an unspecified number of integers, determines how many...

    Java programming 1. Write a program that reads an unspecified number of integers, determines how many positive and negative value 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: (red indicates a user input) Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is...

  • Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a...

    Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a program that reads unspecified number of integers , determines how many negative and positive values have been read. Also calculate total and average. Your program will end with input 0. • Output Enter an integer, the input ends if it is 0: 25 34 -89 72 -35 -67 21 48 0 The number of positives is 5 The number of negatives is 3 The...

  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (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. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • I need to write a program that reads an unspecified number of scores and determines how...

    I need to write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average. Enter a negative number to signify the end of the input. Assume that the maximum number of scores is 100. PreLab07 Analyze scores Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how...

  • in C The preceding LML program reads two numbers from the keyboard and determines and prints the larger value. Note the use of the instruction +4107 as a conditional trans- fer of control, much...

    in C The preceding LML program reads two numbers from the keyboard and determines and prints the larger value. Note the use of the instruction +4107 as a conditional trans- fer of control, much the same as C's if statement. In-Class Tasks Write LML programs that accomplish each of the following tasks: Use a sentinel-controlled loop to read 10 positive integers and compute and print their sum 1. 2. Use a counter-controlled loop to read seven numbers, some positive and...

  • (5 Marks) 3 Write a C sentinel-controlled while loop that will compute the sum of integers...

    (5 Marks) 3 Write a C sentinel-controlled while loop that will compute the sum of integers entered by the user ending with 999. Include all declaration and initialization of variables. Also, output the sum.

  • Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link...

    Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link to the file: CountAndAverageNumbers.javaPreview the document. Make sure you include comments in your code where errors were corrected. If you do not flag each error with a comment, points will be deducted. Upload the corrected .java file here. The output of the corrected file looks like the following: Debug4Output.PNG This program reads an unspecified number of integers, determines how many positive and negative values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT