6.7k views
5 votes
Write a program to read a list of nonnegative integers and to display the largest integer, the smallest integer, and the average of all the integers. The user indicates the end of the input by entering a negative sentinel value that is not used in finding the largest, smallest, and average values. The average should be a value of type double so that it is computed with a fractional part. Input Notes: The input is simply a sequence of positive integers, separated by white space and terminated by -1.

User Mezbaul
by
6.3k points

1 Answer

3 votes

Answer:

The program in Python is as follows:

mynum = []

num = int(input("Enter a number: "))

total = 0

while num >= 0:

mynum.append(num)

total+=num

num = int(input("Enter a number: "))

print("Smallest: ",min(mynum))

print("Largest: ",max(mynum))

print("Average: ",total/len(mynum))

Step-by-step explanation:

This creates an empty list

mynum = []

This prompts the user for input

num = int(input("Enter a number: "))

This initializes total to 0

total = 0

The following is repeated until a negative number is inputted

while num >= 0:

This appends the inputted number into the list

mynum.append(num)

This calculates the sum of all the numbers inputted

total+=num

This prompts the user for another input

num = int(input("Enter a number: "))

This calculates and prints the smallest

print("Smallest: ",min(mynum))

This calculates and prints the largest

print("Largest: ",max(mynum))

This calculates and prints the average

print("Average: ",total/len(mynum))

User Amit Dayama
by
6.7k points