Signup/Sign In

Python Program to find max of two numbers

To learn how to find the maximum of two numbers using the Python programming language. Our aim is to write a program where we shall take two integers as input from the user and return the maximum out of the two integers.

We will use if-else which are conditional statements to compare the two numbers and find the greatest of them. If you are not familiar with different conditional statements in Python, I advise you to refer to this tutorial on conditional statements. For example,

Look at the sample input and output mentioned below

Input- num1= 4 , num2= 8

Output- max=8

To understand the working of the code, have a look at the algorithm:


Step 1- Define a function max_of_two_num() to find max of two numbers

Step 2- Pass two numbers as parameters

Step 3- Use if condition to compare two numbers

Step 4- If the condition is true return the number which is compared

Step 5- Else return the other number

Step 6- Print max of two numbers

Python Program

Look at the code below to understand the working.

def max_of_two_num(n,m):
    if n>m:
        return n
        return m   

a=int(input("Enter first number:"))
b=int(input("Enter second number:"))


print("Maximum of",a,"and",b,"is",x)

Enter first number:5
Enter second number:9
Maximum of 5 and 9 is 9


In this tutorial, we have learned how to use if-else statements to find the maximum of two numbers in Python. We defined a function where we took two numbers as input from the user and passed it in the function. The function used these parameters to find the greater of the two numbers and returned the greater number. The value returned by the function was stored in a variable declared by us and then used to print the result.