Monday, October 15, 2018

Python program to find Max Min number

Python program to find maximum and minimum numbers from the all input numbers. Terminate the input by entering a negative number.

Method 1
Simple Python program is given below using simple programming concepts.

num=int(input("Enter a Number?")) # enter first number
max=num;
min=num;
while  num>=0: #  repetition to get max and min, get other numbers until enter negative
    if num > max:
        max=num
    if num < min:
        min=num
    num=int(input("Enter a Number?"))
print("Maximum number is %d" % max)
print("Minimum number is %d" % min)

Note
The above program is written using only basics concepts without any data structure in Python.
No need to define variables in python.
Indentations are used in Python for blocking statements.
#  is used for comments

Method 2
Python program using List data structure where List data Structure is having inbuilt max(), min() functions.

num=eval(input('Enter a number?'))
L=[]
while  num>=0:
    L.append(num)
    num=eval(input('Enter a number?'))
if len(L)!=0:
    print("Maximum number is %d" % max(L))
    print("Minimum number is %d" % min(L))

Note
append() is a method of List in Python and max(), min() are again inbuilt functions of List.


No comments:

Post a Comment