Monday, 21 October 2019

Write a algorithm and python function program to find minimum and maximum in a list without using built in function min() and max() through looping :

#user defined function
def max_min(n):
    ls=[]
    for i in range(n):
        nu=int(input("enter the number"))
        ls.append(nu)
    print(ls)
    l=ls[0]
    for j in range(n):
        if(ls[j]>l):
            l=ls[j]
    print("larger=",l)

    s=ls[0]
    for j in range(n):
        if(ls[j]<s):
            s=ls[j]
    print("smaller=",s)
       


#main program
n=int(input("Enter the size of the list="))
max_min(n)


output
Enter the size of the list=4
enter the number6
enter the number8
enter the number2
enter the number3
[6, 8, 2, 3]
larger= 8
smaller= 2

No comments:

Post a Comment