18. WAP to accept a number from the user and print the frequency of
the
number in the list. If number is not in the list, it should print
"Number
not available". (answer should be in python)
Answers
Answer:
This is the required program for the question in Python.
1. Using user-defined logic.
n=int(input("How many elements for the list? "))
l=[]
print("Enter them..")
for i in range(n):
l.append(int(input("Enter: ")))
c=0
y=int(input("Enter the number to count frequency: "))
for i in l:
if y==i:
c+=1
if c==0:
print("Not in List.")
else:
print("Frequency:",c)
Explanation: Ask the user to enter the elements in the list. Again, ask the user to enter the number whose frequency is to be calculated. Then iterate a loop in the list range. Check if the number is equal to the entered number then increment the value of count by 1. At last, check if count = 0 or not. If true, display the mes.sage that number is not in the list or else, display the frequency of the number.
2. Using inbuilt function.
The list .count() function counts the frequency of elements. Using this function, problem is solved.
n=int(input("How many elements for the list? "))
l=[]
print("Enter them..")
for i in range(n):
l.append(int(input("Enter: ")))
y=int(input("Enter the number to count frequency: "))
c=l.count(y)
if c==0:
print("Not in List.")
else:
print("Frequency:",c)
See the attachment for output ☑.