Python program to find the largest even and odd numbers in a list

Write a Python Program to print largest Even and Odd numbers in a list :

In this tutorial, we will write one python 3 program to find out the largest odd and even number in a list. User will enter all the numbers to store in the list. Next we will run one loop to find out the largest even and odd numbers. Following algorithm we are going to use in this example :

Algorithm using to find out the largest even and odd numbers :

1. Ask the user how many numbers he wants to add in the list.
2. Create one empty array to store all numbers
3. Run one ‘for‘ loop and get all the numbers from the user
4. Create two variables to store largest even and largest odd number
5. Now, run one for loop and scan all numbers of the user input list
6. Check for each number : if it is even and greater than the stored largest even number , store it as largest even. Do the same thing for even number also.
7. After all elements are scanned, print the largest odd and largest even number.

Python 3 Program :

# get the total numbers to be stored in the list
total_numbers = int(input("How my numbers you want to add to the list : "))

# create one empty array to store the numbers
numbers_array = []

# run a loop and get the inputs from the user
for i in range(0,total_numbers):
	numbers_array.append(int(input("Number to add : ")))

# create two variables to store largest even and odd number of the list
# store -1 to both of these variables
largest_even = -1
largest_odd = -1

# Now scan the array again and update the largest value if found
for i in range(0,total_numbers):
	if(numbers_array[i] % 2 == 0 and numbers_array[i] > largest_even):
		# even number
		largest_even = numbers_array[i]
	elif(numbers_array[i] % 2 != 0 and numbers_array[i] > largest_odd):
		# odd number
		largest_odd = numbers_array[i]


# All numbers are scanned. Now print the largest odd and even value

print("Largest Odd Number : ",largest_odd)
print("Largest Even Number : ",largest_even)

Example Output :

How my numbers you want to add to the list : 5
Number to add : 10
Number to add : 14
Number to add : 3
Number to add : 99
Number to add : 20
Largest Odd Number :  99
Largest Even Number :  20

How my numbers you want to add to the list : 10
Number to add : 10
Number to add : 9
Number to add : 8
Number to add : 7
Number to add : 6
Number to add : 5
Number to add : 4
Number to add : 3
Number to add : 2
Number to add : 1
Largest Odd Number :  9
Largest Even Number :  10

Similar way, we can also find out the smallest even and smallest odd number using python. Try with different inputs and let me know if you have any queries.

Sitewide-USD 336x280

Leave a Reply