Find the largest/smallest number in a list/tuple
- Details
- Category: 11th Class computer practicals
- Published: Friday, 11 June 2021 06:01
- Written by Super User
- Hits: 413
numbers=[3,1,6,7,8,4]
size=len(numbers)
i=0
max=0
while(i<size):
if(numbers[i]>max):
max=numbers[i]
i=i+1
print('max number is ',max)
Explanation: In this program we have taken a list named numbers, we have some integers in it. then we took size of the list or we can say number of elements in the list. initialized variable i to 0 for loop counter. max variable to store max value is also taken and initialized to 0.
Then program enters the while loop checking the condition, we are checking each number with max variable and if its greater than max, then max will now store the value of number. loop counter is increased by 1.this process goes on until all numbers are checked against max variable. program comes out of loop and prints max number on screen.