×

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers

We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them. This python program needs the user to enter two different values. Next, Python will compare these numbers and print the greatest among them, or both are equal.

Example:

Input: A = 5, B = 6  Output: B is greater than A

Source Code

The following is the source code that print the greatest amongst them or both numbers are equal:

 #Take two number from user to compare                     
 num1 = int(input("Enter first number: "))      
 num2 = int(input("Enter second number: ")) 
 #Compare both the number 
 if num1 >= num2:   
     if num1 == num2:    
         print("Both numbers are equal.")       
     else:  
         print("Fisrt number is greater than the second number.")
 else:   
     print("Second number is greater than the First number.") 

Output

Case 1:

Python program to find the greatest among two numbers

Case 2:

Python program to find the greatest among two numbers

Case 3:

Python program to find the greatest among two numbers

Explanation: In the above code, we have asked a user to take two integer values as an input and store them in a separate variable. After taking the inputs, it will compare both the numbers and check that which number is greater or both the number is equal by using if....else statement. After performing the comparison, the final result will be printed on the screen.

Largest of Two Numbers using Arithmetic Operator

In this Python programs, we are using a Minus operator to find the greatest of Two Numbers:

#Program to find the greatest among two number using operators  
#Take two number from user to compare    
num1 = int(input("Enter first number: "))  
num2 = int(input("Enter second number: "))   
#Compare both the number 
 if(num1 - num2 > 0): 
print("{0} is largest than {1}".format(num1, num2)) 
elif(num2 - num1 > 0):     
print("{0} is largest than {1}".format(num2, num1)) 
else:     print("Both numbers are Equal")

Output

Here is the result:

Python program to find the greatest among two numbers

Related Topics

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Add a key-value pair to dictionary in Python

In programming, data type defines the type of value that a variable can hold. With help of these, we can perform various mathematical, logical, or relational operations on that particular...

5 minutes read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 minutes read.

How to get Time in seconds in Python

Python's time module source shows that are based on time. The time() method is used by developers and returns the current time as a UNIX timestamp. A UNIX timestamp is...

4 minutes read.

How to slice a list in python

When working with lists, we face situations where we may need a part of the list from one index to the other. Slicing is one of the simpler ways to...

5 minutes read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

GET and POST requests using Python

‘GET’ and ‘POST’ are two request methods of Hypertext Transfer Protocol (HTTP). What is HTTP? HTTP stands for Hypertext Transfer Protocol. It is a collection of protocols which makes communication between client...

4 minutes read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Python Setup guide and installation

Python Installation guide Step by Step Python is widely used high-level programming language. The first version of Python was launched in 1991. Since then, Python has been gaining popularity. It is considered as...

4 minutes read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Anaconda python 3.7 download for windows 10 64-bit

Before installing Anaconda, you need to first know what Anaconda actually is. Anaconda is an IDE (Integrated development environment) platform used to code and executes python programs. Anaconda works both online...

3 minutes read.

Python vars() Function

Python vars() Function The vars() function in Python returns the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute. Syntax vars([object]) Parameter object: This parameter represents any object with a __dict__attribute Return This function returns...

1 minute read.

Python Selectors

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

SKLearn Model Selection

The model selections of SK learn has many functions with which we can work on.It has functions to cross-validate the model and, it also provides validation and learning curves.It is...

3 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.