Whileloop controlled by a sentinel value Write a program tha

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

Solution

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

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site