Python program maximum of three
Last Updated :
06 Sep, 2024
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”.
Python
# 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))
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.
Python
# 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))
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.
Python
# 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))
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.
Python
# 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))
Time Complexity: O(N log N), where N is the length of the list
Auxiliary Space: O(1)
Please Login to comment...