146k views
5 votes
Answer all in CODE

For this part of the assignment you will be creating a loop that reads in integer values entered by a user using the console until they enter a negative number, at which point the loop should quit and display (print out) the following information:
a) count - the number of iterations made by the loop
b)total - the total of all values entered (excluding the negative value)
c)minimum - the smallest number entered by the user

1 Answer

2 votes

Final answer:

To read in integer values using a console until a negative number is entered, use a while loop with count, total, and minimum variables. After the loop ends, print the count, total sum excluding the negative number, and minimum value entered.

Step-by-step explanation:

To create a loop that reads integer values from the user until a negative number is entered, you can use a while loop in a programming language like Python. The loop should include variables to keep track of the count, total sum of values entered, and the minimum value. Here is an example code snippet that does this:
count = 0
total = 0
minimum = float('inf') # Set to infinity so any number entered will be smaller

while True:
number = int(input('Enter an integer value (negative to quit): '))
if number < 0:
break
count += 1
total += number
if number < minimum:
minimum = number

print(f'Count: {count}')
print(f'Total: {total}')
print(f'Minimum: {minimum}')

When the user enters a negative number, the loop exits, and the program prints out the count, total (not including the negative number), and the minimum value entered.

User David Aleu
by
7.2k points