How to find Maximum of Two numbers in Python?

Hello Programmers, In this blog post, We will learn to find the Maximum of two numbers in python. We will discuss three different methods to find maximum of two numbers in python. Lets discuss them in detail:- 

Maximum of two numbers in python

Example:-

Input :- A= 12 , B= 15

Output :- 15

 

Input :- num1 = -3, num2 = -9

Output :- -3

1. Simple Program to find Maximum:-

This is the simplest method to find the maximum number from the given two numbers. In this method, we will use if else statement to find the greatest number. 

simple.py

# Program to find Max of two numbers
a = 12
b = 18
print(“The greatest number is: “)
if a>b:
    print(a)
else:
    print(b)

OUTPUT

The greatest number is:
18

2. Using Def and user Input Method:-

In this method, we will use the def function to define the greatest function with if else statement in it. Once the function is defined we will now add two number which will take input from user and that you numbers will be defined in the def’s greatest function that was made earlier.

userdef.py

# program to find maximum of two numbers
def greatest(a,b):
     if a>b:
          print(a, “is bigger” )
     else:
          print(b, “is bigger” )
num1 = int( input(“Enter the value of 1st number : “) )
num2 = int( input(“Enter the value of 2nd number : “) )
greatest(num1, num2)

OUTPUT

Enter the value of 1st number : 45
Enter the value of 2nd number : 14
45 is bigger

3. Using Max() Function:-

In this method, we will use max() function to find the maximum of two numbers in python. Firstly we will declare two number of our choice. After declaring the function we will make a variable maximum which will be equal to max(a,b). whenever the maximum variable will be printed it will show the maximum number of both the numbers. 

Max.py

# Python program to find maximum of two numbers
a = 12
b = 13
maximum = max(a, b)
print( “The maximum of both number is:”, maximum )

OUTPUT

The maximum of both number is: 13

Hope this blog post was helpful to you and you have got the answer to your question. Thank you for visiting our blog. If you have any doubts about any coding questions then let us know in the comments section we will answer them as soon as possible. Till then check out our more blog posts.

Leave a Comment

Your email address will not be published. Required fields are marked *