Ask Question
8 April, 08:17

Count positive and negative number and compute the average. The program will have the user input an unspecified number of integers.

Determine how many are positive and negative.

Compute the total and average. Use a while loop.

Output as follows:

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

The number of positives: 3

The number of negatives: 1

The total is 5.0

The average is 1.25

+3
Answers (1)
  1. 8 April, 12:04
    0
    The solution code is written in Python 3

    total = 0 count = 0 neg = 0 pos = 0 num = int (input ("Enter an integer: ")) while (num! = 0) : total + = num count + = 1 if (num < 0) : neg + = 1 else: pos + = 1 num = int (input ("Enter an integer: ")) print ("The number of positives: " + str (pos)) print ("The number of negatives: " + str (neg)) print ("The total is " + str (total)) print ("The average is " + str (total/count))

    Explanation:

    Firstly, we create four variables, total, count, neg and pos (Line 1 - 4). This is to prepare the variable to hold the value of summation of input integer (total), total number of input number (count), total negatives (neg) and total positives (pos).

    Next, we prompt user for the first integer (Line 6).

    Create a sentinel while loop and set the condition so long as the current input number, num is not equal to zero. the program will just keep adding the current num to total (Line 9) and increment the count by one (Line 10).

    if num smaller than zero, increment the neg by one (Line 13) else increment the pos by one (Line 15). This is to track the total number of positives and negatives.

    Finally, we can display all the required output (Line 20 - 23) using the Python built-in function print () when user enter 0 to terminate the while loop. The output shall be as follows:

    The number of positives: 3

    The number of negatives: 1

    The total is 5

    The average is 1.25
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Count positive and negative number and compute the average. The program will have the user input an unspecified number of integers. ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers