Write a python program to input two numbers and display the larger / smaller number.

This article is created to cover some programs in Python, that find and prints largest or greatest between two numbers entered by user. Here are the list of approaches to do the job:

  • Find Largest of Two Numbers using if-else
  • Using if only
  • Using user-defined Function

Find Largest of Two Numbers using if-else

To find largest or greatest of two numbers in Python, you have to ask from user to enter any two numbers, then using if-else statement, find and print the largest one as shown in the program given below:

The question is, write a Python program to find largest between two numbers using if-else. Here is its answer:

print("Enter Two Numbers: ") numOne = int(input()) numTwo = int(input()) if numOne>numTwo: print("\nLargest Number =", numOne) else: print("\nLargest Number =", numTwo)

Here is its sample run:

find largest of two number python

Now supply inputs say 30 as first number, press ENTER key, then enter 40 as second number and press ENTER key to find and print largest of two numbers like shown in the snapshot given below:

print largest of two number python

The dry run of above program with same user input as provided in sample run, goes like:

  • Initial values, numOne=30 (entered by user), numTwo=40 (also entered by user)
  • Now the condition (of if) numOne>numTwo or 30>40 evaluates to be false, therefore program flow goes inside the body of its counterpart, that is else, and prints the value of numTwo as largest number

Modified Version of Previous Program

This is the modified version of previous program. This program handles with invalid and equal inputs both. That is, when user enters two numbers like c and $, or 4 as both first and second number:

print("Enter Two Numbers: ", end="") try: numOne = int(input()) try: numTwo = int(input()) print() if numOne>numTwo: print(numOne, ">", numTwo) elif numTwo>numOne: print(numTwo, ">", numOne) else: print(numOne, "=", numTwo) except ValueError: print("\nInvalid Input!") except ValueError: print("\nInvalid Input!")

Here is its sample run with user input, 50 and 50 as first and second number:

largest of two number python

And here is another sample run with two numbers input as 39 and 74:

python find largest of two numbers

Find Largest of Two Numbers using if Only

This program is created using only if statement. Here using if, I've applied all three conditions. That is, whether first number is greater than second, whether second number is greater than first, or whether both numbers are equal.

print("Enter Two Numbers: ", end="") numOne = int(input()) numTwo = int(input()) if numOne>numTwo: print("\nLargest Number =", numOne) if numTwo>numOne: print("\nLargest Number =", numTwo) if numOne==numTwo: print("\nBoth Numbers are Equal")

Here is its sample run with user input 100 and 100 as both numbers:

python program greatest of two numbers

Find Largest of Two Numbers using Function

This is the last program of this article, created using a user-defined function named FindLargOfTwo(). It takes two arguments and returns 1, if first argument's value is greater, returns 2 if second argument's value is greater.

def FindLargOfTwo(x, y): if x>y: return 1 elif x<y: return 2 print("Enter Two Numbers: ", end="") numOne = int(input()) numTwo = int(input()) res = FindLargOfTwo(numOne, numTwo) if res==1: print("\nLargest Number =", numOne) elif res==2: print("\nLargest Number =", numTwo) else: print("\nBoth Numbers are Equal")

Same Program in Other Languages

Python Online Test

« Previous Program Next Program »

Like/Share Us on Facebook 😋

In the program below, we've used the + operator to add two numbers.

Example 1: Add Two Numbers

# This program adds two numbers num1 = 1.5 num2 = 6.3 # Add two numbers sum = num1 + num2 # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

Output

The sum of 1.5 and 6.3 is 7.8

The program below calculates the sum of two numbers entered by the user..

Example 2: Add Two Numbers With User Input

# Store input numbers num1 = input('Enter first number: ') num2 = input('Enter second number: ') # Add two numbers sum = float(num1) + float(num2) # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

Output

Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8

In this program, we asked the user to enter two numbers and this program displays the sum of two numbers entered by user.

We use the built-in function input() to take the input. Since, input() returns a string, we convert the string into number using the float() function. Then, the numbers are added.

Alternative to this, we can perform this addition in a single statement without using any variables as follows.

print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))

Output

Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8

Although this program uses no variable (memory efficient), it is harder to read.

Python Program to get two numbers num1 and num2 and find the smallest one among num1 and num2.

Sample Input 1:

5 6

Sample Output 1:

5

Program or Solution

num1=int(input("Enter your first number:")) num2=int(input("Enter your second number: ")) if(num1<num2): print("{} is smallest".format(num1)) elif(num2<num1): print("{} is smallest".format(num2)) else: print("{} and {} are equal".format(num1,num2))

Program Explanation

Get two inputs num1 and num2 from user using input() statement check whether num1 is smaller than num2 using if statement.

if num1 is smaller print num1 using print() method, else check whether num2 is smaller than num1 using elseif statement.

If num2 is smaller print num2 using print() method, else print num1 and num2 are equal using print() statment.

# Python Program to input 2 numbers and display larger number

#input first number num1=int(input("Enter First Number")) #input Second number num2=int(input("Enter Second Number")) #Check if first number is greater than second if (num1>num2): print("The Larger number is", num1) else: print ("The Larger number is", num2)

Output:

Enter First Number23 Enter Second Number35 The Larger number is 35 >>>

# Python Program to input 2 numbers and display Smaller number

#input first number num1=int(input("Enter First Number")) #input Second number num2=int(input("Enter Second Number")) #Check if first number is lesser than second if (num1<num2): print("The Smaller number is", num1) else: print ("The Smaller number is", num2)

Output:

Enter First Number23 Enter Second Number35 The Smaller number is 23 >>>