Q. Write a program that returns the largest even number in the list of integers. If there is no even number in the input, print "No even element".


Examples:
With list[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9] it will print 6
With list(3, -1001] it will print "No even element" 20
With list[ 6, 18, 23, 20, 29] it will print 20

Write the program code.


Answer :-
lst = eval (input("Enter the number list :- "))
newlst = []
for i in lst :
    if i % 2 == 0 :
        newlst += [ i ]
if newlst == [] :
    print ("No even element")
else :
    newlst.sort()
    print ("Largest even number in the list is :- ", newlst[-1])

Output :-

Enter the number list :- [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9]
Largest even number in the list is :-  6

>>>

Enter the number list :- [1,3,5,7,9]
No even element

>>>

8 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. I tried doing but its showing error

    ReplyDelete
  2. In else block, we don't need to sort it, we need to print largest value, so just use max sir/madam

    ReplyDelete
  3. yo guys here are some interestings facts
    Snakes can predict earthquakes. ...
    Lego mini-figures have the largest population on Earth! ...
    Astronauts grow taller in space! ...
    We spend a year on the toilet in our lifetime. ...
    There's a 50% chance that two people will share a birthday in a group of 23 people.

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post