Sunday, September 22, 2024
Google search engine
HomeLanguagesPython program maximum of three

Python program maximum of three

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


Output

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


Output

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


Output

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


Output

14

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

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?