Skip to main content

Python Program to Find Largest & Smallest among N Numbers

In Python there are many ways to identify largest or smallest number from user input. Here we are using for loop.

nrange = int(input("\n\t From how many numbers you want to find smallest & largest number\t"))
num = int(input("\n\t Enter number 1 ==>\t"))
min_num = num
max_num = num
for x in range(nrange-1):
 
    num=int(input('\n\t Enter number %d ==>\t'%(x+2)))
    if ( num < min_num ):
        min_num = num
    if ( num > max_num ):
        max_num = num

print('\n\t Maximum number is %d'%(max_num))
print('\n\t Minimum number is %d'%(min_num))

Output


	 From how many numbers you want to find smallest & largest number	3
3

	 Enter number 1 ==>	3

	 Enter number 2 ==>	3

	 Enter number 3 ==>	4

	 Maximum number is 4

	 Minimum number is 3
>>> 

In above program we are accepting first number from user and declaring it as maximum and minimum number.

Further in for loop we are accepting numbers from user and comparing it with variables maximum and minimum to interchange.

Recommended to write logic on paper and execute it manually then use editor.