Python Program to exchange the smallest and largest number in an array of N integers.

a = [0]*20
min = 0
max = 0
temp1 = 0
temp2 = 0
pos1 = 0
pos2 = 0
print("Enter the num of elements : ")
n = int(input())
print("Enter the elements :")
for i in range(n) :
	a[i] = int(input())
	if (i == 0) :
		min = max = a[i]
	if (a[i]<min) :
		min = a[i]
	elif (a[i]>max) :
		max = a[i]
print("Biggest element is ",max," and Smallest elementis ",min,"\n ")
for i in range(n) :
	if (min == a[i]) :
		pos1 = i
		break
for i in range(n) :
	if (max == a[i]) :
		pos2 = i
		break
temp1 = a[pos1]
temp2 = a[pos2]
a[pos1] = temp2
a[pos2] = temp1
print("Array after interchange of smallest and largest : ")
for i in range(n) :
	print("Element : ", a[i])

Output :
Enter the num of elements : 5
Enter the elements :
10
20
30
40
50

Biggest element is 50 and Smallest elementis 10

Array after interchange of smallest and largest :

Element : 50
Element : 20
Element : 30
Element : 40
Element : 10



Leave a comment