31.4k views
0 votes
7.3 (Count occurrence of numbers) Write a program that reads the integers between 1 and 50 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program:

User JTE
by
5.8k points

1 Answer

5 votes

Answer:

The programming language is not stated;

However, the program in python is as follows;

mylist = []

user_input = int(input("Input: "))

while not user_input == 0:

if 1<=user_input<=50:

mylist.append(user_input)

user_input = int(input("Input: "))

for i in range(1,51):

if i in mylist:

print(str(i)+" occured "+str(mylist.count(i))+" times")

Step-by-step explanation:

This line creates an empty list

mylist = []

This line gets input from the user

user_input = int(input("Input: "))

This line begins an iteration that checks if user input is not 0

while not user_input == 0:

This if condition checks if user input is within range of 1 to 50

if 1<=user_input<=50:

If both conditions are true, the user input is appended to the list

mylist.append(user_input)

This line gets another input from the user

user_input = int(input("Input: "))

This following loop prints the occurrence of each number in the list

for i in range(1,51):

if i in mylist:

print(str(i)+" occured "+str(mylist.count(i))+" times")

User Mark Cibor
by
5.7k points