Given three numbers a b and c, the task is that have to find the greatest element among in given numberĀ
Examples:
Input: a = 2, b = 4, c = 3 Output: 4 Input: a = 4, b = 2, c = 6 Output: 6
Python Program to Fine the Largest Among Three Numbers
A simple program to get the maximum from three numbers using Python āandā.Ā
Python3
# Python program to find the largest # number among the three numbers Ā
def maximum(a, b, c): Ā
Ā Ā Ā Ā if (a > = b) and (a > = c): Ā Ā Ā Ā Ā Ā Ā Ā largest = a Ā
Ā Ā Ā Ā elif (b > = a) and (b > = c): Ā Ā Ā Ā Ā Ā Ā Ā largest = b Ā Ā Ā Ā else : Ā Ā Ā Ā Ā Ā Ā Ā largest = c Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return largest Ā
Ā
# Driven code a = 10 b = 14 c = 12 print (maximum(a, b, c)) |
14
Time Complexity:Ā O(1)
Auxiliary Space: O(1)
Python program maximum of three using List
Initialize three numbers by n1, n2, and n3. Add three numbers into the list lst = [n1, n2, n3]. . Using the max() function to find the greatest number max(lst). And finally, we will print the maximum number.
Python3
# Python program to find the largest number # among theĀ three numbers using library function def maximum(a, b, c): Ā Ā Ā Ā list = [a, b, c] Ā Ā Ā Ā return max ( list ) Ā
Ā
# Driven code a = 10 b = 14 c = 12 print (maximum(a, b, c)) |
14
Time Complexity:Ā O(1)
Auxiliary Space: O(1)
Python program maximum of three using Python max
Here, the Python max function helps us to get the max number among the three numbers.
Python3
# Python program to find the largest number # among theĀ three numbers using library function Ā
# Driven code a = 10 b = 14 c = 12 print ( max (a, b, c)) |
14
Time Complexity: Ā O(1)
Auxiliary Space: O(1)
Python program maximum of three using Python sort()Ā
Here, we are using Python sort, and then using list[-1] we are returning the last element from the list.Ā
Python3
# Python program to find the largest number # among the three numbers using library function def maximum(a, b, c): Ā Ā Ā Ā list = [a, b, c] Ā Ā Ā Ā list .sort() Ā Ā Ā Ā return list [ - 1 ] Ā
# Driven code a = 10 b = 14 c = 12 print (maximum(a, b, c)) |
14
Time Complexity: O(N log N), where N is the length of the list
Auxiliary Space: O(1)