This is a Python Program to print the largest even and largest odd number in a list.
The program takes in a list and prints the largest even and largest off number in it.
1. Take in the number of elements to be in the list from the user.
2. Take in the elements from the user using a for loop and append to a list.
3. Using a for loop, get the elements one by one from the list and check if it odd or even and append them to different lists.
4. Sort both the lists individually and get the length of each list.
5. Print the last elements of the sorted lists.
Here is source code of the Python Program to print the largest even and largest odd number in a list. The program output is also shown below.
n=int(input("Enter the number of elements to be in the list:")) b= for i in range(0,n): a=int(input("Element: ")) b.append(a) c= d= for i in b: if(i%2==0): c.append(i) else: d.append(i) c.sort() d.sort() count1=0 count2=0 for k in c: count1=count1+1 for j in d: count2=count2+1 print("Largest even number:",c[count1-1]) print("Largest odd number",d[count2-1])
1. User must enter the number of elements to be in the list.
2. User must then enter the elements of the list one by one which is appended to the list.
3. The for loop is used to traverse through the list and obtain the elements one by one.
4. The if statement checks whether the element is odd or even.
5. The odd elements are appended to another list and the even elements are appending to another list.
6. Both the lists are then sorted.
7. The individual lengths of both the lists is obtained using for loops.
8. The last element of the sorted lists which is the largest odd and largest even number is printed.
Case 1: Enter the number of elements to be in the list:5 Element: 45 Element: 20 Element: 80 Element: 93 Element: 3 Largest even number: 80 Largest odd number 93 Case 2: Enter the number of elements to be in the list:4 Element: 23 Element: 10 Element: 34 Element: 89 Largest even number: 34 Largest odd number 89
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.