+ 1
Question: Input 10 numbers and find largest odd number.
Can anyone help me to answer this Question, ask the user to input 10 integers, and then prints the largest odd number without using max function. hope i can see the answer in Python.
5 Réponses
+ 2
#Enter the numbers seperated by space.
print(sorted([i for i in map(int,input().split()) if i%2])[-1])
+ 2
Lhak Chung
If you tried it you would have seen that it does only print the largest one.
The [-1] at the end only references one item.
The sorted makes sure that the last item of list is largest.
The listcomp removes even numbers.
+ 1
I don't know python but i am sure that the easiest way to do is using a for loop and a variable called bigger. If the variables are stored in an array it should look like this
int bigger = 0
for(int i =0;i<10;i++)
if(num[i] %2 !=0 && num[i] > bigger)
bigger=num[i]
print(bigger)
You can change it to make it look like python
0
thanks
0
@Louis okay, but I only want to print largest one, thanks anyway