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
The following codes have been written using Python.
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 function, which calculates the length of the list. The sum and the average of the list are then printed.