find out Largest number from a list using loop

finding largest number from a given list is one such program that almost every computer student try. Python has some built-in method to solve this mystery but as a student, they must know the working of this method. So here in this program, we are going to solve this mystery using the traditional method as well as python way.

Finding largest number using tradition method ( Looping method )

In this method we assign the first available number into a variable and check with the remaining values, if any value is larger then our stored largest number then assign this new number as our largest number. The source code of largest number is as follows

numbers =[]
for i in range(10):
    x = int (input('Enter any number '))
    number.append(x)

largest = number[0] 
for i in range(1,10):
    if largest<number[i]:
       largest = number[i]

print('Largest number :', largest)

The above code is using looping concept to find our largest number.

Second solution is coming via Python max( ) function

numbers =[] 
for i in range(10): 
    x = int (input('Enter any number ')) 
    number.append(x) 

largest = max(number)
print('Largest number :',largest)

Above Source code is using Max() function available in python. The function automatically return the largest number available in the list.

Third solution – using sort( ) method

numbers =[]
for i in range(10): 
      x = int (input('Enter any number ')) 
      number.append(x)

number.sort() 
largest = number[-1] 
print('Largest number :',largest)

Since we are sorting our list in ascending order thus the largest number will be placed at -1 index in the list and the same we are storing in our variable largest.

All these solutions will find out the largest number from a list of numbers. Choose any method that you like to find out largest number from a list.

 

 

 

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.