Computer Science, asked by singhsourv2901, 1 day ago

Write a program that receives a series of numbers from the user and allows the user to press the enter key to indicate that he or she is finished providing inputs. After the user presses the enter key, the program should print the sum of the numbers and their average.

Answers

Answered by Equestriadash
1

The following co‎des have been written using Python.

\tt l\ =\ list()\\while\ True:\\{\ \ \ \ \ }n\ =\ in put("Enter\ a\ number: ")\\{\ \ \ \ \ }if\ n\ ==\ "":\\{\ \ \ \ \ }{\ \ \ \ \ }break\\{\ \ \ \ \ }else:\\{\ \ \ \ \ }{\ \ \ \ \ }l.append(int(n))\\{\ \ \ \ \ }{\ \ \ \ \ }continue\\s\ =\ 0\\for\ i\ in\ l:\\{\ \ \ \ \ }s\ =\ s\ +\ i\\print(s,\ "is\ the\ sum\ of\ the\ numbers\ entered.")\\print(s/len(l),\ "is\ the\ average\ of\ the\ numbers\ entered.")

We first initialize a list for the numbers to be stored in. When then start a loop that repeatedly asks for input, in the form of a string value. We use a conditional statement to check if the string is empty, i.e., if the user has pressed the Enter key instead of a number. If it is empty, the loop stops. If not, the numbers are added to the list earlier initialized for the same purpose, in its integer form, in order to perform calculations, and continues. Once the data retrieval is done, we assign a variable to act as the sum of the numbers in the list and we start another loop to add all the elements of the list. The average is calculated by taking the sum stored in 's', and the number of elements in the list, that can be taken using the \tt len() function, which calculates the length of the list. The sum and the average of the list are then printed.

Similar questions