Python Program to Print Largest Even and Largest Odd Number in a List

This is a Python Program to print the largest even and largest odd number in a list.

Problem Description

The program takes in a list and prints the largest even and largest off number in it.

Problem Solution

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.
6. Exit.

Program/Source Code

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])
Program Explanation

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.

advertisement
advertisement
Runtime Test Cases
 
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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.