Python program to find maximum of two numbers

Here, we are going to learn how to find the maximum number of two numbers with different methods in Python?
Submitted by Shivang Yadav, on April 03, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Here, we will take input from the users (two numbers) and return the maximum of them using different methods in Python.

Example:

Input:
num1 = 54, num2 = 23

Output:
54

Python provides its users multiple ways to perform a task, and for finding the maximum of two number, we have these methods,

  1. Using if-else statement
  2. Using max() function
  3. Using ternary operator

1) Find maximum of two numbers using if-else statement

In this method, we will use if-else ladders with conditional operators to find the greatest values of the two numbers.

Algorithm:

  1. Get input from the user for num1 and num2.
  2. Using if-else ladder to get the maximum value,
    1. if(num1 > num2), print "num1 is greater".
    2. ElseIf (num1 < num2), "num2 is greater".
    3. Else, print "both are equal".

Program to print maximum number using if-else ladder

# Python program to return maximum of two numbers

# Getting input from user
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

# printing the maximum value
if(num1 > num2):
  print(num1, "is greater")
elif(num1 < num2):
    print(num2, "is greater")
else:
    print("Both are equal")

Output:

RUN 1:
Enter the first number: 12
Enter the second number: 34
34 is greater

RUN 2:
Enter the first number: 34
Enter the second number: 12
34 is greater

RUN 3:
Enter the first number: 12
Enter the second number: 12
Both are equal

Explanation:

In the above code, we have taken input from the user on two numbers num1 and num2. Then using the if-elif-else statement, we have printed greater values.

2) Find maximum of two numbers using max() function

In this method, we will use the max() function from python's built-in library.

max() function:

The max() function in Python is used to return the maximum of all the elements passed as arguments.

Syntax:

max(arg1, arg2, arg3, ...)

Arguments: The method can accept any number of values.

Return Type: the method returns the largest value out of all values.

Program to find the maximum of two numbers using max() method

# Python program to return maximum of two numbers

# Getting input from user
num1 = int(input("Enter the num1: "))
num2 = int(input("Enter the num2: "))

# printing the maximum value
maxVal = max(num1, num2)
print("The maximum of all values is", maxVal)

Output:

RUN 1:
Enter the num1: 12
Enter the num2: 34
The maximum of all values is 34

RUN 2:
Enter the num1: 34
Enter the num2: 12
The maximum of all values is 34

RUN 3:
Enter the num1: 12
Enter the num2: 12
The maximum of all values is 12

Explanation:

In the above code, we have taken two integer values as input from the user (num1, num2). And then using the max() method, we have initialized the maxVal and then printed the maximum value.

3) Find maximum of two numbers using ternary operator

In this method, we will use a ternary operator in Python to print the maximum value of two numbers.

Ternary Operator is a single line condition that can be used in place of if-else statement.

Python program to find the maximum of two numbers using ternary operator

# Python program to return maximum of two numbers

# Getting input from user
num1 = int(input("Enter the num1: "))
num2 = int(input("Enter the num2: "))

# printing the maximum value
print("The maximum of all values is", (num1 if num1 >= num2 else num2 ))

Output:

RUN 1:
Enter the num1: 12
Enter the num2: 34
The maximum of all values is 34

RUN 2:
Enter the num1: 34
Enter the num2: 12
The maximum of all values is 34

RUN 3:
Enter the num1: 12
Enter the num2: 12
The maximum of all values is 12

Explanation:

In the above code, we have taken two integer values as input from the user (num1, num2). And then using the ternary operator, we have printed the maximum value.

Python Basic Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.