In the previous article we explored a problem involving a python game utilizing Even or Odd Logic, now we need to write a program to accept three numbers from the user and then automatically provide to us the largest of the three numbers. Suppose the user provides the program the numbers: a=10, b= 15, c=7 it should return to us the variable b along with it’s value. If the numbers provided are a=23, b=-1 and c= -28. The variable a and the value 23 is returned. Suppose it is a=5, b=5. c=5, the program returns the value of 5.
Implementation of Largest of Three numbers program in Python
Let us see a simple working of a python program that accepts three values from the user: a, b and c . And then it prints out the largest of the three numbers. It figures out the largest of the three by utilizing conditional if-else statements and comparison operators.
a=int(input("Enter First Number: ")) b=int(input("Enter Second Number: ")) c=int(input("Enter Third Number: ")) if(a>=b) and (a>=c): print(a) if(b>=a) and (b>=c): print(b) if(c>=a) and (c>=b): print(c)
Output: Enter First Number: 200 Enter Second Number: 400 Enter Third Number: 6000 The largest of the three numbers is 6000
The program returns the largest number found through the use of comparison operators. However there is a similar but way more simple and effective way of finding out the largest of given numbers through the use of the in-built function ‘max( )‘.
max() in python
a=int(input("Enter First Number: ")) b=int(input("Enter Second Number: ")) c=int(input("Enter Third Number: ")) print(max(a,b,c))
Output: Enter First Number: 200 Enter Second Number: 7000 Enter Third Number: 6000 7000
The max( ) function can take in any number of inputs for finding out max value.
Hence, we have seen two approaches for finding out the maximum or largest of a series of given input numbers. In the next article we will look at Leap Year program in Python logic.