Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program maximum of three

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 13 Mar, 2023
Improve Article
Save Article

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

Method 1 (Simple) 

Python3




# Python program to find the largest 
# number among the three numbers 
  
def maximum(a, b, c): 
  
    if (a >= b) and (a >= c): 
        largest =
  
    elif (b >= a) and (b >= c): 
        largest =
    else
        largest =
          
    return largest 
  
  
# Driven code 
a = 10
b = 14
c = 12
print(maximum(a, b, c)) 

Output

14

Time Complexity:  O(1)
Auxiliary Space: O(1)

Method 2 (Using List). Initialize three numbers by n1, n2 and n3 . Add three numbers into list lst = [n1, n2, n3]. . Using max() function to find the greatest number max(lst). . And finally we will print 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))

Output

14

Time Complexity:  O(1)
Auxiliary Space: O(1)

Method 3 (Using max function) 

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))

Output

14

Time Complexity:  O(1)
Auxiliary Space: O(1)

Method 4 Using sort() method

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))

Output

14

Time Complexity: O(N log N), where N is the length of the list
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!