Program which takes list of numbers as input and finds: a) The largest number in the list b) The smallest number in the list c) Product of all the items in the list

Program which takes list of numbers as input and finds:
a) The largest number in the list
b) The smallest number in the list
c) Product of all the items in the list

tl = int(input('number of element:'))
l = []
for i in range(tl):
    e = eval(input('element:'))
    l.append(e)

large_num = max(l)
small_num = min(l)
p = 1
for item in l:
    p = p * item

print('largest number in list:', large_num)
print('smallest number in list:', small_num)
print('product of all items in list:', p)
Output : 
number of element: 4
element: 2
element: 5
element: 1
element: 3

largest number in list: 5
smallest number in list: 1
product of all item in list: 30

Explanation

  1. Prompt for Total Number of Elements (tl=int(input('number of element:'))):
    • This line asks the user to enter the total number of elements they want in the list. The input is converted to an integer and stored in the variable tl.
  2. Initialize an Empty List (l=[]):
    • An empty list l is created to store the elements that the user will input.
  3. Input Elements and Append to List:
    • The for loop iterates tl times. In each iteration:
      • The user is prompted to input an element (e=eval(input('element:'))).
      • The input element e is appended to the list l using l.append(e).
  4. Find the Largest and Smallest Numbers:
    • large_num = max(l) finds the largest number in the list l.
    • small_num = min(l) finds the smallest number in the list l.
  5. Calculate the Product of All Elements:
    • p is initialized to 1 (p=1).
    • The for loop iterates through each item in the list l and multiplies it to p (p = p * item). This computes the product of all the elements.
  6. Print the Results:
    • The largest number is printed using print('largest number in list:', large_num).
    • The smallest number is printed using print("smallest number in list:", small_num).
    • The product of all elements is printed using print("product of all item in list:", p).

Leave a Reply

Your email address will not be published. Required fields are marked *