149k views
0 votes
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.

User Todd Li
by
5.3k points

2 Answers

4 votes

Answer:

The code is given below in Java. Follow the code and question for better understanding.

Step-by-step explanation:

import java.util.Scanner;

public class NumbersStats {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int pos = 0, neg = 0, count = 0, num;

double total = 0;

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

while (true) {

num = in.nextInt();

if(num == 0) break;

if(num > 0) {

pos++;

} else {

neg++;

}

total += num;

count++;

}

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

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

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

System.out.println("The average is " + (total/count));

}

}

User Radato
by
5.2k points
0 votes

Answer:

The program to this question can be given as:

Program:

import java.util.*;

//import package for user input

class Main //define class

{

public static void main(String a[])

//define main function

{

int positive_number=0,negative_number=0,count=0,num; //define variable

double total=0,avg=0;

//creating Scanner class object.

Scanner ob = new Scanner(System.in);

System.out.println("Enter an integer, when done input 0: "); //message

num= ob.nextInt();

//taking input from user

if (num==0) //check number equal to 0

{

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

System.exit(1);

}

else

{

while (num!= 0)

{

if (num> 0)

{

positive_number++; // Increase positives

}

else

{

negative_number++; // Increase negatives

}

total=total+num; // Accumulate total

count++; // Increase the count

num=ob.nextInt();

}

// Calculate the average

avg=total/count;

// Display values

System.out.println("The positive number is:"+positive_number);

System.out.println("The negatives number is:"+negative_number);

System.out.println("total is:"+total);

System.out.println("average is:"+avg);

}

}

}

Output:

Enter an integer, when done input 0: 22

2

1

4

0

The positive number is:4

The negatives number is:0

total is:29.0

average is:7.25

Explanation:

In the above program firstly we import the package for user input then we define a class that is main in this class we define the main method in the main method we define variable. Then we create a scanner class object for user input. In the number variable, we take multiple inputs from the user and also check that the user does not insert 0 at the starting of the program. To check this we use the condition statement that is a number equal to 0 then it will terminate the program. In the else part we first declare the loop that checks that inserted number is positive and negative and in this, we calculate the total of the numbers and at the end of the loop, we calculate the average of the number and print all the values.

User Ymi
by
5.0k points