Saturday, November 16, 2024
Google search engine
HomeLanguagesPython Program to Find Smallest/Minimum of n Numbers

Python Program to Find Smallest/Minimum of n Numbers

Find smallets/Minimum number of n numbers in python; In this tutorial, you will learn how to find the minimum number in a list of given n numbers in python.

.medrectangle-3-multi-320{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

Python Program to Find Smallest/Minimum of n Numbers

Let’s use the following algorithm to write a program to find the minimum number in a list of given n numbers in python:

  • Python program to find smallest of n numbers using min
  • Python program to find smallest of n numbers without using min

Python program to find smallest of n numbers using min

  • Take input number for the length of the list using python input() function.
  • Initialize an empty list lst = [].
  • Read each number in your python program using a for loop.
  • In the for loop append each number to the list.
  • Use built-in python function min() to find the smallest element in a list.
  • End of the program print the smallest number from list.
lst = []

num = int(input('How many numbers: '))

for n in range(num):
    numbers = int(input('Enter number '))
    lst.append(numbers)
    
print("Maximum element in the list is :", min(lst))

Output

How many numbers:  5
Enter number  6
Enter number  4
Enter number  2
Enter number  8
Enter number  9
Minimum element in the list is : 2

Python program to find smallest of n numbers without using min

  • Take input number for the length of the list using python input() function.
  • Initialize an empty list lst = [].
  • Read each number in your python program using a for loop.
  • In the for loop append each number to the list.
  • Define a custom function, which is an accepted number list and used to find the smallest number from list.
  • Call this custom function and store the function result.
  • End of the program print the smallest number from list.
def find_min( list ):
    min = list[ 0 ]
    for a in list:
        if a < min:
            min = a
    return min


num = int(input('How many numbers: '))

lst = []

for n in range(num):
    numbers = int(input('Enter number '))
    lst.append(numbers)
    
print("Minimum element in the list is :", find_min(lst))

Output

How many numbers:  4
Enter number  1
Enter number  5
Enter number  8
Enter number  9
Minimum element in the list is : 1

Recommended Python Programs

RELATED ARTICLES

Most Popular

Recent Comments