Python Program to print largest odd and even number in a list

In this simple python program, we need to sort odd and even numbers in a list. It is an array-based python program.

To understand this example, you should have knowledge of the following Python programming topics:

How to sort odd and even number lists?

In this simple python program, we need to find and print the largest odd and even number from the numbers list. After accepting the numbers from the user, using a for loop in python and append the elements in the list using the append method.

Using a for loop from the start to the end of the list, check each element is divisible by 2 using the mod operator. If the condition is a success, then add that element into another list for even numbers list. If the if condition' fails, then add that number to the odd list. 

How to find the largest number of the list in python?

Now we have 2 separate lists for odd numbers and even numbers. Then apply the sort function in python language in which default is ascending in order. So the last element will be the largest on the list. To get that last element, we use a count variable and increment the count every time we see a number in the list using for loop. Finally, print the number using count -1, which will be the largest number in both lists. 

ALGORITHM

STEP 1: Accept the number of elements in the list using the input function in the python programming language.

STEP 2: Initialize a list called b[] and open a for loop to append the elements to the list using the append operator.

STEP 3: initialize two separate lists for odd numbers and even numbers.

STEP 4: Using a for loop in python language, check each element in the list.

STEP 5: Use an if condition to check the element is divisible by 2. if so, append that element to an even list. 

STEP 6: Else append the element to the odd list using python basic syntax. 

STEP 7: Using the sorting method, sort the elements of both odd and even list, also initialize two count variables

STEP 8: Open two for loops separately for each list and increment the count with 1 to reach the last element of the list, because the sort method sort in ascending order as default. 

STEP 9: Print the count -1 element of the list of both odd and even to print the largest element.

Python Source Code

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

OUTPUT

Enter the number of elements to be in the list:5
Element: 4
Element: 2
Element: 8
Element: 9
Element: 3
Largest even number: 8
Largest odd number 9