226k views
2 votes
(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. Python

User Boulder
by
2.9k points

1 Answer

6 votes

Answer:

Step-by-step explanation:

Sorry it is in Java, though you can covert it using converter

public class Exercise {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int positives = 0; // Count the number of positive numbers

int negatives = 0; // Count the number of negative numbers

int count = 0; // Count all numbers

double total = 0; // Accumulate a totol

// Promopt the user to enter an integer or 0 to exit

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

int number = input.nextInt();

if (number == 0) { // Test for sentinel value

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

System.exit(1);

}

while (number != 0) {// Test for sentinel value

if (number > 0)

positives++; // Increase positives

else

negatives++; // Increase negatives

total += number; // Accumulate total

count++; // Increase the count

number = input.nextInt();

}

// Calculate the average

double average = total / count;

// Display results

System.out.println(

"The number of positive is " + positives +

"\\The number of negatives is " + negatives +

"\\The total is total " + total +

"\\The average is " + average);

}

}